Fix to remove timer when daemon is destroyed
[platform/core/uifw/tts.git] / server / ttsd_engine_agent.c
1 /*
2 *  Copyright (c) 2012, 2013 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
15 #include <dlfcn.h>
16 #include <dirent.h>
17
18 #include "ttsd_main.h"
19 #include "ttsd_engine_agent.h"
20 #include "ttsd_config.h"
21
22 #define ENGINE_PATH_SIZE        256
23
24 /*
25 * Internal data structure
26 */
27 typedef struct {
28         /* base info */
29         char*   engine_uuid;
30         char*   engine_name; 
31         char*   engine_path;
32
33         /* info for using engine load*/
34         bool    is_set;
35         bool    is_loaded;              
36         bool    need_network;
37         void    *handle;
38
39         /* engine base setting */
40         char*   default_lang;
41         int     default_vctype;
42         int     default_speed;
43
44         ttspe_funcs_s*  pefuncs;
45         ttspd_funcs_s*  pdfuncs;
46
47         int (*ttsp_load_engine)(const ttspd_funcs_s* pdfuncs, ttspe_funcs_s* pefuncs);
48         int (*ttsp_unload_engine)();
49 } ttsengine_s;
50
51 typedef struct {
52         char*   engine_uuid;
53         char*   engine_path;
54         char*   engine_name;
55         char*   setting_ug_path;
56         bool    use_network;
57 } ttsengine_info_s;
58
59
60 /** Init flag */
61 static bool g_agent_init;
62
63 /** TTS engine list */
64 static GList *g_engine_list;            
65
66 /** Current engine information */
67 static ttsengine_s g_cur_engine;
68
69 /** Result callback function */
70 static synth_result_callback g_result_cb;
71
72
73 /** Set current engine */
74 int __internal_set_current_engine(const char* engine_uuid);
75
76 /** Check engine id */
77 int __internal_check_engine_id(const char* engine_uuid);
78
79 /** Update engine list */
80 int __internal_update_engine_list();
81
82 /** Get engine info */
83 int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info);
84
85 /** Callback function for result */
86 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data);
87
88 /** Callback function for voice list */
89 bool __supported_voice_cb(const char* language, ttsp_voice_type_e type, void* user_data);
90
91 /** Free voice list */
92 void __free_voice_list(GList* voice_list);
93
94 /** Callback function for engine info */
95 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
96                       bool use_network, void* user_data);
97
98 /** Callback fucntion for engine setting */
99 bool __engine_setting_cb(const char* key, const char* value, void* user_data);
100
101
102 int ttsd_engine_agent_init(synth_result_callback result_cb)
103 {
104         /* initialize static data */
105         if (result_cb == NULL) {
106                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] invalid parameter");
107                 return TTSD_ERROR_INVALID_PARAMETER;
108         }
109
110         g_result_cb = result_cb;
111
112         g_cur_engine.engine_uuid = NULL;
113         g_cur_engine.engine_name = NULL;
114         g_cur_engine.engine_path = NULL;
115
116         g_cur_engine.is_set = false;
117         g_cur_engine.handle = NULL;
118         g_cur_engine.pefuncs = (ttspe_funcs_s*)g_malloc0( sizeof(ttspe_funcs_s) );
119         g_cur_engine.pdfuncs = (ttspd_funcs_s*)g_malloc0( sizeof(ttspd_funcs_s) );
120
121         g_agent_init = true;
122
123         if (0 != ttsd_config_get_default_voice(&(g_cur_engine.default_lang), &(g_cur_engine.default_vctype))) {
124                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default voice in config"); 
125                 /* Set default voice */
126                 g_cur_engine.default_lang = strdup("en_US");
127                 g_cur_engine.default_vctype = TTSP_VOICE_TYPE_FEMALE;
128         }
129
130         if (0 != ttsd_config_get_default_speed(&(g_cur_engine.default_speed))) {
131                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default speed in config"); 
132                 ttsd_config_set_default_speed((int)TTSP_SPEED_NORMAL);
133                 g_cur_engine.default_speed = TTSP_SPEED_NORMAL;
134         }
135
136         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Initialize Engine Agent");
137
138         return 0;
139 }
140
141 int ttsd_engine_agent_release()
142 {
143         if (false == g_agent_init) {
144                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
145                 return TTSD_ERROR_OPERATION_FAILED;
146         }
147
148         /* unload current engine */
149         ttsd_engine_agent_unload_current_engine();
150
151         /* release engine list */
152         GList *iter = NULL;
153         ttsengine_s *data = NULL;
154
155         if (g_list_length(g_engine_list) > 0) {
156                 /* Get a first item */
157                 iter = g_list_first(g_engine_list);
158                 while (NULL != iter) {
159                         /* Get data from item */
160                         data = iter->data;
161                         iter = g_list_remove(iter, data);
162                 }
163         }
164         g_list_free(iter);
165         /* release current engine data */
166         if (g_cur_engine.pefuncs != NULL)
167                 g_free(g_cur_engine.pefuncs);
168         
169         if (g_cur_engine.pdfuncs != NULL)
170                 g_free(g_cur_engine.pdfuncs);
171
172         g_result_cb = NULL;
173         g_agent_init = false;
174
175         if (g_cur_engine.default_lang != NULL) 
176                 g_free(g_cur_engine.default_lang);
177
178
179         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Release Engine Agent");
180
181         return 0;
182 }
183
184 int ttsd_engine_agent_initialize_current_engine()
185 {
186         if (false == g_agent_init) {
187                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
188                 return TTSD_ERROR_OPERATION_FAILED;
189         }
190
191         /* update engine list */
192         if (0 != __internal_update_engine_list()) {
193                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] No engine error");
194                 return TTSD_ERROR_OPERATION_FAILED;
195         }
196
197         /* 2. get current engine from config */
198         char* cur_engine_uuid = NULL;
199         bool is_get_engineid_from_config = false;
200
201         if (0 != ttsd_config_get_default_engine(&cur_engine_uuid)) {
202                 /*not set current engine */
203                 /*set system default engine*/
204                 GList *iter = NULL;
205                 ttsengine_info_s *data = NULL;
206
207                 if (g_list_length(g_engine_list) > 0) {
208                         iter = g_list_first(g_engine_list);
209                         char* default_engine = "27F277E9-BBC4-4dca-B553-D9884A3CDAA0";
210
211                         while (NULL != iter) {
212                                 data = iter->data;
213
214                                 if (0 == strncmp(data->engine_uuid, default_engine, strlen(default_engine))) {
215                                         /* current data is default engine */
216                                         break;
217                                 }
218
219                                 iter = g_list_next(iter);
220                         }
221                 } else {
222                         SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to set a engine of engine list");
223                         return TTSD_ERROR_OPERATION_FAILED;     
224                 }
225
226                 if (NULL != data->engine_uuid) {
227                         cur_engine_uuid = strdup(data->engine_uuid); 
228                 } else {
229                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Data of current engine is corrupt");
230                         return TTSD_ERROR_OPERATION_FAILED;
231                 }
232                 
233                 is_get_engineid_from_config = false;
234         } else {
235                 is_get_engineid_from_config = true;
236         }
237
238         /* check whether cur engine uuid is valid or not. */
239         if (0 != __internal_check_engine_id(cur_engine_uuid)) {
240                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] It is not valid engine id from config");
241
242                 GList *iter = NULL;
243                 
244                 if (g_list_length(g_engine_list) > 0) 
245                         iter = g_list_first(g_engine_list);
246                 else {
247                         SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] NO TTS Engine !!");
248                         return TTSD_ERROR_OPERATION_FAILED;     
249                 }
250
251                 if (cur_engine_uuid != NULL)    
252                         g_free(cur_engine_uuid);
253
254                 ttsengine_info_s *data = NULL;
255                 data = iter->data;
256
257                 cur_engine_uuid = g_strdup(data->engine_uuid);
258
259                 is_get_engineid_from_config = false;
260         }
261
262         if (NULL != cur_engine_uuid) 
263                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
264         else 
265                 return TTSD_ERROR_OPERATION_FAILED;
266
267         /* set current engine */
268         if (0 != __internal_set_current_engine(cur_engine_uuid)) {
269                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
270                 return TTSD_ERROR_OPERATION_FAILED;
271         } 
272
273         if (false == is_get_engineid_from_config) {
274                 if (0 != ttsd_config_set_default_engine(cur_engine_uuid))
275                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config"); 
276         }
277
278         if (NULL != cur_engine_uuid)    
279                 g_free(cur_engine_uuid);
280
281         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine"); 
282
283         return 0;
284 }
285
286 int __internal_check_engine_id(const char* engine_uuid)
287 {
288         GList *iter = NULL;
289         ttsengine_s *data = NULL;
290
291         if (g_list_length(g_engine_list) > 0) {
292                 iter = g_list_first(g_engine_list);
293
294                 while (NULL != iter) {
295                         data = iter->data;
296
297                         if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid))) 
298                                 return 0;
299         
300                         iter = g_list_next(iter);
301                 }
302         }
303
304         return -1;
305 }
306
307 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
308                              bool use_network, void* user_data)
309 {
310         ttsengine_info_s* temp = (ttsengine_info_s*)user_data; 
311
312         temp->engine_uuid = g_strdup(engine_uuid);
313         temp->engine_name = g_strdup(engine_name);
314         temp->setting_ug_path = g_strdup(setting_ug_name);
315         temp->use_network = use_network;
316 }
317
318 int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
319 {
320         char *error;
321         void* handle;
322
323         handle = dlopen (filepath, RTLD_LAZY );
324
325         if (!handle) {
326                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath); 
327                 return TTSD_ERROR_OPERATION_FAILED;
328         }
329
330         /* link engine to daemon */
331         dlsym(handle, "ttsp_load_engine");
332         if ((error = dlerror()) != NULL) {
333                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine. Fail to open ttsp_load_engine : %s", filepath);
334                 dlclose(handle);
335                 return TTSD_ERROR_OPERATION_FAILED;
336         }
337
338         dlsym(handle, "ttsp_unload_engine");
339         if ((error = dlerror()) != NULL) {
340                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine. Fail to open ttsp_unload_engine : %s", filepath);
341                 dlclose(handle);
342                 return TTSD_ERROR_OPERATION_FAILED;
343         }
344
345         int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
346
347         get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
348         if (NULL != (error = dlerror()) || NULL == get_engine_info) {
349                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] ttsp_get_engine_info() link error");
350                 dlclose(handle);
351                 return TTSD_ERROR_OPERATION_FAILED;
352         }
353
354         ttsengine_info_s* temp;
355         temp = (ttsengine_info_s*)g_malloc0(sizeof(ttsengine_info_s));
356
357         /* get engine info */
358         if (0 != get_engine_info(&__engine_info_cb, (void*)temp)) {
359                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get engine info");
360                 dlclose(handle);
361                 g_free(temp);
362                 return TTSD_ERROR_OPERATION_FAILED;
363         }
364
365         /* close engine */
366         dlclose(handle);
367
368         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
369                 if (true == temp->use_network) {
370                         free(temp);
371                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent WARNING] %s is invalid because of network based engine.", temp->engine_name);
372                         return TTSD_ERROR_OPERATION_FAILED;
373                 }
374         }
375
376         temp->engine_path = g_strdup(filepath);
377         
378         SLOG(LOG_DEBUG, get_tag(), "----- Valid engine");
379         SLOG(LOG_DEBUG, get_tag(), "Engine uuid : %s", temp->engine_uuid);
380         SLOG(LOG_DEBUG, get_tag(), "Engine name : %s", temp->engine_name);
381         SLOG(LOG_DEBUG, get_tag(), "Setting ug path : %s", temp->setting_ug_path);
382         SLOG(LOG_DEBUG, get_tag(), "Engine path : %s", temp->engine_path);
383         SLOG(LOG_DEBUG, get_tag(), "Use network : %s", temp->use_network ? "true":"false");
384         SLOG(LOG_DEBUG, get_tag(), "-----");
385         SLOG(LOG_DEBUG, get_tag(), "  ");
386
387         *info = temp;
388
389         return 0;
390 }
391
392 int __internal_update_engine_list()
393 {
394         /* relsease engine list */
395         GList *iter = NULL;
396         ttsengine_info_s *data = NULL;
397
398         if (g_list_length(g_engine_list) > 0) {
399                 iter = g_list_first(g_engine_list);
400
401                 while (NULL != iter) {
402                         data = iter->data;
403
404                         if (data != NULL)
405                                 g_free(data);
406
407                         g_engine_list = g_list_remove_link(g_engine_list, iter);
408                         iter = g_list_first(g_engine_list);
409                 }
410         }
411
412         /* get file name from engine directory and get engine information from each filename */
413         DIR *dp;
414         struct dirent *dirp;
415         dp = opendir(ENGINE_DIRECTORY_DEFAULT);
416
417         if (dp != NULL) {
418                 while ((dirp = readdir(dp)) != NULL) {
419                         ttsengine_info_s* info;
420                         char* filepath = NULL;
421                         int file_size;
422
423                         file_size = strlen(ENGINE_DIRECTORY_DEFAULT) + strlen(dirp->d_name) + 5;
424                         filepath = (char*)g_malloc0( sizeof(char) * file_size);
425
426                         if (NULL != filepath) { 
427                                 strncpy(filepath, ENGINE_DIRECTORY_DEFAULT, strlen(ENGINE_DIRECTORY_DEFAULT) );
428                                 strncat(filepath, "/", strlen("/") );
429                                 strncat(filepath, dirp->d_name, strlen(dirp->d_name) );
430                         } else {
431                                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
432                                 continue;       
433                         }
434
435                         /* get its info and update engine list */
436                         if (0 == __internal_get_engine_info(filepath, &info)) {
437                                 /* add engine info to g_engine_list */
438                                 g_engine_list = g_list_append(g_engine_list, info);
439                         }
440
441                         if (NULL != filepath)
442                                 g_free(filepath);
443                 }
444
445                 closedir(dp);
446         }
447
448         dp = opendir(ENGINE_DIRECTORY_DOWNLOAD);
449
450         if (dp != NULL) {
451                 while ((dirp = readdir(dp)) != NULL) {
452                         ttsengine_info_s* info;
453                         char* filepath = NULL;
454                         int file_size;
455
456                         file_size = strlen(ENGINE_DIRECTORY_DOWNLOAD) + strlen(dirp->d_name) + 5;
457                         filepath = (char*)g_malloc0( sizeof(char) * file_size);
458
459                         if (NULL != filepath) { 
460                                 strcpy(filepath, ENGINE_DIRECTORY_DOWNLOAD);
461                                 strcat(filepath, "/");
462                                 strcat(filepath, dirp->d_name);
463                         } else {
464                                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
465                                 continue;       
466                         }
467
468                         /* get its info and update engine list */
469                         if (0 == __internal_get_engine_info(filepath, &info)) {
470                                 /* add engine info to g_engine_list */
471                                 g_engine_list = g_list_append(g_engine_list, info);
472                         }
473
474                         if (NULL != filepath)
475                                 g_free(filepath);
476                 }
477
478                 closedir(dp);
479         }
480
481         if (g_list_length(g_engine_list) <= 0) {
482                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Engine");
483                 return TTSD_ERROR_OPERATION_FAILED;     
484         }
485
486 #ifdef ENGINE_AGENT_DEBUG       
487         ttsd_print_enginelist();
488 #endif
489
490         return 0;
491 }
492
493 int __internal_set_current_engine(const char* engine_uuid)
494 {
495         /* check whether engine id is valid or not. */
496         GList *iter = NULL;
497         ttsengine_info_s *data = NULL;
498
499         bool flag = false;
500         if (g_list_length(g_engine_list) > 0) {
501                 iter = g_list_first(g_engine_list);
502
503                 while (NULL != iter) {
504                         data = iter->data;
505
506                         if (0 == strncmp(data->engine_uuid, engine_uuid, strlen(engine_uuid))) {
507                                 flag = true;
508                                 break;
509                         }
510
511                         /*Get next item*/
512                         iter = g_list_next(iter);
513                 }
514         }
515
516         /* If current engine does not exist, return error */
517         if (false == flag) {
518                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] __internal_set_current_engine : Cannot find engine id");
519                 return TTSD_ERROR_OPERATION_FAILED;
520         } else {
521                 if (g_cur_engine.engine_uuid != NULL) {
522                         /*compare current engine uuid */
523                         if (0 == strncmp(g_cur_engine.engine_uuid, data->engine_uuid, strlen(engine_uuid))) {
524                                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] tts engine has already been set");
525                                 return 0;
526                         }
527                 }
528         }
529
530         /* set data from g_engine_list */
531         if (g_cur_engine.engine_uuid != NULL)   g_free(g_cur_engine.engine_uuid);
532         if (g_cur_engine.engine_name != NULL)   g_free(g_cur_engine.engine_name);
533         if (g_cur_engine.engine_path != NULL)   g_free(g_cur_engine.engine_path);
534
535         if (NULL == data->engine_uuid || NULL == data->engine_name || NULL == data->engine_path) {
536                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] __internal_set_current_engine : Engine data is NULL");
537                 return TTSD_ERROR_OPERATION_FAILED;
538         }
539
540         g_cur_engine.engine_uuid = g_strdup(data->engine_uuid);
541         g_cur_engine.engine_name = g_strdup(data->engine_name);
542         g_cur_engine.engine_path = g_strdup(data->engine_path);
543
544         g_cur_engine.handle = NULL;
545         g_cur_engine.is_loaded = false;
546         g_cur_engine.is_set = true;
547         g_cur_engine.need_network = data->use_network;
548
549         SLOG(LOG_DEBUG, get_tag(), "-----");
550         SLOG(LOG_DEBUG, get_tag(), "Current engine uuid : %s", g_cur_engine.engine_uuid);
551         SLOG(LOG_DEBUG, get_tag(), "Current engine name : %s", g_cur_engine.engine_name);
552         SLOG(LOG_DEBUG, get_tag(), "Current engine path : %s", g_cur_engine.engine_path);
553         SLOG(LOG_DEBUG, get_tag(), "-----");
554
555         return 0;
556 }
557
558 int ttsd_engine_agent_load_current_engine()
559 {
560         if (false == g_agent_init) {
561                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
562                 return TTSD_ERROR_OPERATION_FAILED;
563         }
564
565         if (false == g_cur_engine.is_set) {
566                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ttsd_engine_agent_load_current_engine : No Current Engine ");
567                 return TTSD_ERROR_OPERATION_FAILED;
568         }
569
570         /* check whether current engine is loaded or not */
571         if (true == g_cur_engine.is_loaded ) {
572                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] ttsd_engine_agent_load_current_engine : Engine has already been loaded " );
573                 return 0;
574         }
575
576         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] current engine path : %s", g_cur_engine.engine_path);
577
578         /* open engine */
579         char *error = NULL;
580         g_cur_engine.handle = dlopen(g_cur_engine.engine_path, RTLD_LAZY); /* RTLD_LAZY RTLD_NOW*/
581
582         if (NULL != (error = dlerror()) || NULL == g_cur_engine.handle) {
583                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get current engine handle : dlopen error ($s)", error);
584                 return -2;
585         }
586
587         g_cur_engine.ttsp_unload_engine = (int (*)())dlsym(g_cur_engine.handle, "ttsp_unload_engine");
588         if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_unload_engine) {
589                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to link daemon to ttsp_unload_engine() of current engine : (%s)", error);
590                 return -3;
591         }
592
593         g_cur_engine.ttsp_load_engine = (int (*)(const ttspd_funcs_s* , ttspe_funcs_s*) )dlsym(g_cur_engine.handle, "ttsp_load_engine");
594         if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_load_engine) {
595                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to link daemon to ttsp_load_engine() of current engine : %s", error);
596                 return -3;
597         }
598
599         /* load engine */
600         g_cur_engine.pdfuncs->version = 1;
601         g_cur_engine.pdfuncs->size = sizeof(ttspd_funcs_s);
602
603         int ret = 0;
604         ret = g_cur_engine.ttsp_load_engine(g_cur_engine.pdfuncs, g_cur_engine.pefuncs); 
605         if (0 != ret) {
606                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to load engine - %s : result(%d)", g_cur_engine.engine_path, ret);
607                 return TTSD_ERROR_OPERATION_FAILED;
608         }
609
610         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] engine info : version(%d), size(%d)",g_cur_engine.pefuncs->version, g_cur_engine.pefuncs->size );
611
612         /* engine error check */
613         if (g_cur_engine.pefuncs->size != sizeof(ttspe_funcs_s)) {
614                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ttsd_engine_agent_load_current_engine : current engine is not valid");
615                 return TTSD_ERROR_OPERATION_FAILED;
616         }
617
618         /* initalize engine */
619         if (NULL == g_cur_engine.pefuncs->initialize) {
620                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] init function of engine is NULL!!");
621                 return TTSD_ERROR_OPERATION_FAILED;
622         }
623
624         ret = g_cur_engine.pefuncs->initialize(__result_cb);
625         if (0 != ret) {
626                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to initialize current engine : result(%d)", ret);
627                 return TTSD_ERROR_OPERATION_FAILED;
628         }
629
630         /* select default voice */
631         bool set_voice = false;
632         if (NULL != g_cur_engine.default_lang) {
633                 if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
634                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] is_valid_voice() of engine is NULL!!");
635                         return TTSD_ERROR_OPERATION_FAILED;
636                 }
637
638                 if (true == g_cur_engine.pefuncs->is_valid_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype)) {
639                         set_voice = true;
640                         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set origin default voice to current engine : lang(%s), type(%d)", 
641                                 g_cur_engine.default_lang,  g_cur_engine.default_vctype);
642                 } else {
643                         SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail set origin default voice : lang(%s), type(%d)",
644                                 g_cur_engine.default_lang, g_cur_engine.default_vctype);
645                 }
646         }
647
648         if (false == set_voice) {
649                 if (NULL == g_cur_engine.pefuncs->foreach_voices) {
650                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_voice_list of engine is NULL!!");
651                         return TTSD_ERROR_OPERATION_FAILED;
652                 }
653
654                 /* get language list */
655                 int ret;
656                 GList* voice_list = NULL;
657
658                 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
659
660                 if (0 == ret && 0 < g_list_length(voice_list)) {
661                         GList *iter = NULL;
662                         voice_s* voice = NULL;
663                         
664                         iter = g_list_first(voice_list);
665
666                         /* check english */
667                         while (NULL != iter) {
668                                 voice = iter->data;
669
670                                 if (NULL != voice) {
671                                         if (0 == strcmp("en_US", voice->language)) 
672                                                 break;
673                                 }
674
675                                 iter = g_list_next(iter);
676                         }
677                         if (NULL == voice) {
678                                 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to find voice in list");
679                                 return TTSD_ERROR_OPERATION_FAILED;
680                         }
681         
682                         /* Set selected language and type */
683                         if (true != g_cur_engine.pefuncs->is_valid_voice(voice->language, voice->type)) {
684                                 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail voice is NOT valid");
685                                 return TTSD_ERROR_OPERATION_FAILED;
686                         }
687
688                         ttsd_config_set_default_voice(voice->language, (int)voice->type);
689                         
690                         g_cur_engine.default_lang = g_strdup(voice->language);
691                         g_cur_engine.default_vctype = voice->type;
692
693                         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Select default voice : lang(%s), type(%d)", 
694                                 voice->language,  voice->type);
695
696                         __free_voice_list(voice_list);
697                 } else {
698                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
699                         return TTSD_ERROR_OPERATION_FAILED;
700                 }
701         } 
702  
703         g_cur_engine.is_loaded = true;
704
705         return 0;
706 }
707
708 int ttsd_engine_agent_unload_current_engine()
709 {
710         if (false == g_agent_init) {
711                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
712                 return TTSD_ERROR_OPERATION_FAILED;
713         }
714
715         if (false == g_cur_engine.is_set) {
716                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
717                 return TTSD_ERROR_OPERATION_FAILED;
718         }
719
720         if (false == g_cur_engine.is_loaded) {
721                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Engine has already been unloaded " );
722                 return 0;
723         }
724
725         /* shutdown engine */
726         if (NULL == g_cur_engine.pefuncs->deinitialize) {
727                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The deinitialize() of engine is NULL!!");
728         } else {
729                 int ret = 0;
730                 ret = g_cur_engine.pefuncs->deinitialize();
731                 if (0 != ret) {
732                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Fail deinitialize() : result(%d)", ret);
733                 }
734         }
735
736         /* unload engine */
737         g_cur_engine.ttsp_unload_engine();
738         
739         dlclose(g_cur_engine.handle);
740
741         /* reset current engine data */
742         g_cur_engine.handle = NULL;
743         g_cur_engine.is_loaded = false;
744
745         return 0;
746 }
747
748 bool ttsd_engine_agent_need_network()
749 {
750         if (false == g_agent_init) {
751                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
752                 return TTSD_ERROR_OPERATION_FAILED;
753         }
754
755         if (false == g_cur_engine.is_loaded) {
756                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
757                 return TTSD_ERROR_OPERATION_FAILED;
758         }
759
760         return g_cur_engine.need_network;
761 }
762
763 bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang, ttsp_voice_type_e* out_type)
764 {
765         if (NULL == lang || NULL == out_lang || NULL == out_type) {
766                 return false;
767         }
768
769         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Select voice : input lang(%s) , input type(%d), default lang(%s), default type(%d)", 
770                 lang, type, g_cur_engine.default_lang, g_cur_engine.default_vctype);
771
772         
773         /* case 1 : Both are default */
774         if (0 == strncmp(lang, "default", strlen("default")) && 0 == type) {
775                 *out_lang = strdup(g_cur_engine.default_lang);
776                 *out_type = g_cur_engine.default_vctype;
777                 return true;
778         } 
779         
780         /* Get voice list */
781         if (NULL == g_cur_engine.pefuncs->foreach_voices) {
782                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] foreach_voices of engine is NULL!!");
783                 return TTSD_ERROR_OPERATION_FAILED;
784         }
785
786         GList* voice_list = NULL;
787         int ret = 0;
788
789         ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
790         if (0 != ret || 0 >= g_list_length(voice_list)) {
791                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
792                 return false;
793         }
794
795         bool result;
796         result = false;
797
798         GList *iter = NULL;
799         voice_s* voice;
800
801         /* lang and type are not default type */
802         if (0 != strncmp(lang, "default", strlen("default")) && 0 != type) {
803                 iter = g_list_first(voice_list);
804
805                 while (NULL != iter) {
806                         /* Get handle data from list */
807                         voice = iter->data;
808
809                         if (0 == strncmp(voice->language, lang, strlen(lang)) &&  voice->type == type) {
810                                 *out_lang = strdup(voice->language);
811                                 *out_type = voice->type;
812                                 result = true;
813                                 break;
814                         }
815
816                         iter = g_list_next(iter);
817                 }
818
819         } else if (0 != strncmp(lang, "default", strlen("default")) && 0 == type) {
820                 /* Only type is default */
821                 if (0 == strncmp(lang, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
822                         *out_lang = strdup(g_cur_engine.default_lang);
823                         *out_type = g_cur_engine.default_vctype;
824                         result = true;
825                 } else {
826                         voice_s* voice_selected = NULL;
827                         iter = g_list_first(voice_list);
828                         while (NULL != iter) {
829                                 /* Get handle data from list */
830                                 voice = iter->data;
831
832                                 if (0 == strncmp(voice->language, lang, strlen(lang))) {
833                                         voice_selected = voice;
834                                         if (voice->type == g_cur_engine.default_vctype) {
835                                                 voice_selected = voice;
836                                                 break;
837                                         }
838                                 }
839                                 iter = g_list_next(iter);
840                         }
841
842                         if (NULL != voice_selected) {
843                                 *out_lang = strdup(voice_selected->language);
844                                 *out_type = voice_selected->type;
845                                 result = true;
846                         }
847                 }
848         } else if (0 == strncmp(lang, "default", strlen("default")) && 0 != type) {
849                 /* Only lang is default */
850                 if (type == g_cur_engine.default_vctype) {
851                         *out_lang = strdup(g_cur_engine.default_lang);
852                         *out_type = g_cur_engine.default_vctype;
853                         result = true;
854                 } else {
855                         voice_s* voice_selected = NULL;
856                         iter = g_list_first(voice_list);
857                         while (NULL != iter) {
858                                 /* Get handle data from list */
859                                 voice = iter->data;
860
861                                 if (0 == strncmp(voice->language, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang)) ) {
862                                         voice_selected = voice;
863                                         if (voice->type == type) {
864                                                 voice_selected = voice;
865                                                 break;
866                                         }
867                                 }
868                                 iter = g_list_next(iter);
869                         }
870
871                         if (NULL != voice_selected) {
872                                 *out_lang = strdup(voice->language);
873                                 *out_type = voice_selected->type;
874                                 result = true;
875                         }
876                 }
877         }
878
879         if (true == result) {
880                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Selected voice : lang(%s), type(%d)", *out_lang, *out_type);
881         }
882
883         __free_voice_list(voice_list);
884
885         return result;
886 }
887
888 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
889 {
890         if (false == g_agent_init) {
891                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
892                 return false;
893         }
894
895         if (false == g_cur_engine.is_loaded) {
896                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
897                 return false;
898         }
899
900         /* compare current engine and engine id.*/
901         if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
902                 return true;
903         }
904
905         return false;
906 }
907
908 int ttsd_engine_agent_set_default_voice(const char* language, ttsp_voice_type_e vctype)
909 {
910         if (false == g_agent_init) {
911                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
912                 return TTSD_ERROR_OPERATION_FAILED;
913         }
914
915         if (false == g_cur_engine.is_loaded) {
916                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
917                 return TTSD_ERROR_OPERATION_FAILED;
918         }
919
920         if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
921                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
922                 return TTSD_ERROR_OPERATION_FAILED;
923         }
924
925         int ret = -1;
926         if(false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
927                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
928                 return TTSD_ERROR_INVALID_VOICE;
929         }
930
931         if (NULL != g_cur_engine.default_lang)
932                 free(g_cur_engine.default_lang);
933
934         g_cur_engine.default_lang = strdup(language);
935         g_cur_engine.default_vctype = vctype;
936
937         ret = ttsd_config_set_default_voice(language, (int)vctype);
938         if (0 == ret) {
939                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
940                         g_cur_engine.default_lang, g_cur_engine.default_vctype); 
941         } else {
942                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to write default voice to config (%d)", ret); 
943                 return TTSD_ERROR_OPERATION_FAILED;
944         }
945
946         return 0;
947 }
948
949 /******************************************************************************************
950 * TTS Engine Interfaces for client
951 *******************************************************************************************/
952
953 int ttsd_engine_start_synthesis(const char* lang, const ttsp_voice_type_e vctype, const char* text, const int speed, void* user_param)
954 {
955         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] start ttsd_engine_start_synthesis()");
956
957         if (false == g_agent_init) {
958                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
959                 return TTSD_ERROR_OPERATION_FAILED;
960         }
961
962         if (false == g_cur_engine.is_loaded) {
963                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
964                 return TTSD_ERROR_OPERATION_FAILED;
965         }
966
967         /* select voice for default */
968         char* temp_lang = NULL;
969         ttsp_voice_type_e temp_type;
970         if (true != ttsd_engine_select_valid_voice(lang, vctype, &temp_lang, &temp_type)) {
971                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to select default voice");
972                 return TTSD_ERROR_INVALID_VOICE;
973         } else {
974                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Start synthesis : language(%s), type(%d), speed(%d), text(%s)", 
975                         temp_lang, temp_type, speed, text);
976         }
977
978         if (NULL == g_cur_engine.pefuncs->start_synth) {
979                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] start_synth() of engine is NULL!!");
980                 return TTSD_ERROR_OPERATION_FAILED;
981         }
982
983         ttsp_speed_e temp_speed;
984
985         if (0 == speed) {
986                 temp_speed = g_cur_engine.default_speed;
987         } else {
988                 temp_speed = speed;
989         }
990
991         /* synthesize text */
992         int ret = 0;
993         ret = g_cur_engine.pefuncs->start_synth(temp_lang, temp_type, text, temp_speed, user_param);
994         if (0 != ret) {
995                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************", ret);
996                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] * synthesize error : result(%6d) *", ret);
997                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************", ret);
998                 return TTSD_ERROR_OPERATION_FAILED;
999         }
1000
1001         if (NULL == temp_lang)
1002                 free(temp_lang);
1003
1004         return 0;
1005 }
1006
1007
1008 int ttsd_engine_cancel_synthesis()
1009 {
1010         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] start ttsd_engine_cancel_synthesis()");
1011         
1012         if (false == g_agent_init) {
1013                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1014                 return TTSD_ERROR_OPERATION_FAILED;
1015         }
1016
1017         if (false == g_cur_engine.is_loaded) {
1018                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1019                 return TTSD_ERROR_OPERATION_FAILED;
1020         }
1021         
1022         if (NULL == g_cur_engine.pefuncs->cancel_synth) {
1023                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] cancel_synth() of engine is NULL!!");
1024                 return TTSD_ERROR_OPERATION_FAILED;
1025         }
1026
1027         /* stop synthesis */
1028         int ret = 0;
1029         ret = g_cur_engine.pefuncs->cancel_synth();
1030         if (0 != ret) {
1031                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail cancel synthesis : result(%d)", ret);
1032                 return TTSD_ERROR_OPERATION_FAILED;
1033         }
1034
1035         return 0;
1036 }
1037
1038 int ttsd_engine_get_audio_format( ttsp_audio_type_e* type, int* rate, int* channels)
1039 {
1040         if (false == g_agent_init) {
1041                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1042                 return TTSD_ERROR_OPERATION_FAILED;
1043         }
1044
1045         if (false == g_cur_engine.is_loaded) {
1046                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1047                 return TTSD_ERROR_OPERATION_FAILED;
1048         }
1049
1050         if (NULL == g_cur_engine.pefuncs->get_audio_format) {
1051                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_audio_format() of engine is NULL!!");
1052                 return TTSD_ERROR_OPERATION_FAILED;
1053         }
1054
1055         int ret = 0;
1056         ret = g_cur_engine.pefuncs->get_audio_format(type, rate, channels);
1057         if (0 != ret) {
1058                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get audio format : result(%d)", ret);
1059                 return TTSD_ERROR_OPERATION_FAILED;
1060         } else
1061                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] get audio format : type(%d), rate(%d), channel(%d)", *type, *rate, *channels);
1062         
1063         return 0;
1064 }
1065
1066 bool __supported_voice_cb(const char* language, ttsp_voice_type_e type, void* user_data)
1067 {
1068         GList** voice_list = (GList**)user_data;
1069
1070         if (NULL == language || NULL == voice_list) {
1071                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
1072                 return false;
1073         }
1074
1075         SLOG(LOG_DEBUG, get_tag(), "-- Language(%s), Type(%d)", language, type);
1076
1077         voice_s* voice = g_malloc0(sizeof(voice_s));
1078         voice->language = strdup(language);
1079         voice->type = type;
1080
1081         *voice_list = g_list_append(*voice_list, voice);
1082
1083         return true;
1084 }
1085
1086 int ttsd_engine_get_voice_list(GList** voice_list)
1087 {
1088         if (false == g_agent_init) {
1089                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1090                 return TTSD_ERROR_OPERATION_FAILED;
1091         }
1092
1093         if (false == g_cur_engine.is_loaded) {
1094                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1095                 return TTSD_ERROR_OPERATION_FAILED;
1096         }
1097
1098         if (NULL == g_cur_engine.pefuncs->foreach_voices) {
1099                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
1100                 return TTSD_ERROR_OPERATION_FAILED;
1101         }
1102
1103         int ret = 0;
1104         ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, (void*)voice_list);
1105         if (0 != ret) {
1106                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get voice list : result(%d)", ret);
1107                 return TTSD_ERROR_OPERATION_FAILED;
1108         }
1109
1110         return 0;
1111 }
1112
1113 int ttsd_engine_get_default_voice( char** lang, ttsp_voice_type_e* vctype )
1114 {
1115         if (false == g_agent_init) {
1116                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1117                 return TTSD_ERROR_OPERATION_FAILED;
1118         }
1119
1120         if (false == g_cur_engine.is_loaded) {
1121                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1122                 return TTSD_ERROR_OPERATION_FAILED;
1123         }
1124
1125         if (NULL == lang || NULL == vctype) {
1126                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] BAD Parameter"); 
1127                 return TTSD_ERROR_INVALID_PARAMETER;
1128         }
1129
1130         if (NULL != g_cur_engine.default_lang) {
1131                 *lang = g_strdup(g_cur_engine.default_lang);
1132                 *vctype = g_cur_engine.default_vctype;
1133
1134                 SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice : language(%s), type(%d)", *lang, *vctype);
1135         } else {
1136                 if (NULL == g_cur_engine.pefuncs->foreach_voices) {
1137                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_voice_list of engine is NULL!!");
1138                         return TTSD_ERROR_OPERATION_FAILED;
1139                 }
1140
1141                 /* get language list */
1142                 int ret;
1143                 GList* voice_list = NULL;
1144
1145                 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
1146
1147                 if (0 == ret && 0 < g_list_length(voice_list)) {
1148                         GList *iter = NULL;
1149                         voice_s* voice;
1150
1151                         iter = g_list_first(voice_list);
1152
1153                         if (NULL != iter) {
1154                                 voice = iter->data;
1155                                 
1156                                 if (true != g_cur_engine.pefuncs->is_valid_voice(voice->language, voice->type)) {
1157                                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail voice is NOT valid ");
1158                                         return TTSD_ERROR_OPERATION_FAILED;
1159                                 }
1160                                 
1161                                 ttsd_config_set_default_voice(voice->language, (int)voice->type);
1162                                 
1163                                 if (NULL != g_cur_engine.default_lang)
1164                                         g_free(g_cur_engine.default_lang);
1165
1166                                 g_cur_engine.default_lang = g_strdup(voice->language);
1167                                 g_cur_engine.default_vctype = voice->type;
1168
1169                                 *lang = g_strdup(g_cur_engine.default_lang);
1170                                 *vctype = g_cur_engine.default_vctype = voice->type;
1171
1172                                 SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice (New selected) : language(%s), type(%d)", *lang, *vctype);
1173                         } else {
1174                                 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
1175                                 return TTSD_ERROR_OPERATION_FAILED;
1176                         }
1177
1178                         __free_voice_list(voice_list);
1179                 } else {
1180                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
1181                         return TTSD_ERROR_OPERATION_FAILED;
1182                 }
1183         }
1184         
1185         return 0;
1186 }
1187
1188 /*
1189 * TTS Engine Interfaces for setting
1190 */
1191 int ttsd_engine_setting_get_engine_list(GList** engine_list)
1192 {
1193         if (false == g_agent_init) {
1194                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1195                 return TTSD_ERROR_OPERATION_FAILED;
1196         }
1197
1198         if (NULL == engine_list) {
1199                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL" );
1200                 return TTSD_ERROR_INVALID_PARAMETER;
1201         }
1202
1203         /* update engine list */
1204         if (0 != __internal_update_engine_list()) {
1205                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail __internal_update_engine_list()");
1206                 return TTSD_ERROR_OPERATION_FAILED;
1207         }
1208
1209         GList *iter = NULL;
1210         ttsengine_info_s *data = NULL;
1211
1212         iter = g_list_first(g_engine_list);
1213
1214         SLOG(LOG_DEBUG, get_tag(), "----- [Engine Agent] engine list -----");
1215         
1216         while (NULL != iter) {
1217                 engine_s* temp_engine;
1218         
1219                 temp_engine = (engine_s*)g_malloc0(sizeof(engine_s));
1220
1221                 data = iter->data;
1222
1223                 temp_engine->engine_id = strdup(data->engine_uuid);
1224                 temp_engine->engine_name = strdup(data->engine_name);
1225                 temp_engine->ug_name = strdup(data->setting_ug_path);
1226
1227                 *engine_list = g_list_append(*engine_list, temp_engine);
1228
1229                 iter = g_list_next(iter);
1230
1231                 SLOG(LOG_DEBUG, get_tag(), " -- engine id(%s) engine name(%s) ug name(%s)", 
1232                         temp_engine->engine_id, temp_engine->engine_name, temp_engine->ug_name);
1233                 
1234         }
1235
1236         SLOG(LOG_DEBUG, get_tag(), "--------------------------------------");
1237         
1238         return 0;
1239 }
1240
1241 int ttsd_engine_setting_get_engine(char** engine_id)
1242 {
1243         if (false == g_agent_init) {
1244                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1245                 return TTSD_ERROR_OPERATION_FAILED;
1246         }
1247
1248         if (false == g_cur_engine.is_loaded) {
1249                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1250                 return TTSD_ERROR_ENGINE_NOT_FOUND;
1251         }
1252
1253         *engine_id = strdup(g_cur_engine.engine_uuid);
1254
1255         return 0;
1256 }
1257
1258 int ttsd_engine_setting_set_engine(const char* engine_id)
1259 {
1260         if (false == g_agent_init) {
1261                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1262                 return TTSD_ERROR_OPERATION_FAILED;
1263         }
1264
1265         /* compare current engine and new engine.*/
1266         if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1267                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] new engine(%s) is the same as current engine(%s)", engine_id, g_cur_engine.engine_uuid);
1268                 return 0;
1269         }
1270
1271         char* tmp_uuid = NULL;
1272         tmp_uuid = strdup(g_cur_engine.engine_uuid);
1273
1274         /* unload engine */
1275         if (0 != ttsd_engine_agent_unload_current_engine()) {
1276                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to unload current engine");
1277         } else {
1278                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] unload current engine");
1279         }
1280
1281         /* change current engine */
1282         if (0 != __internal_set_current_engine(engine_id)) {
1283                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail __internal_set_current_engine()");
1284
1285                 /* roll back to old current engine. */
1286                 __internal_set_current_engine(tmp_uuid);
1287                 ttsd_engine_agent_load_current_engine();
1288                 
1289                 if (tmp_uuid != NULL)   
1290                         free(tmp_uuid);
1291
1292                 return TTSD_ERROR_OPERATION_FAILED;
1293         }
1294
1295         /* load engine */
1296         if (0 != ttsd_engine_agent_load_current_engine()) {
1297                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to load new engine");
1298                 
1299                 if( tmp_uuid != NULL )  
1300                         free(tmp_uuid);
1301
1302                 return TTSD_ERROR_OPERATION_FAILED;
1303         } else {
1304                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load new engine");
1305         }
1306
1307         /* save engine id to config */
1308         if (0 != ttsd_config_set_default_engine(engine_id)) {
1309                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to save engine id to config"); 
1310         }
1311
1312         if (tmp_uuid != NULL)   
1313                 free(tmp_uuid);
1314
1315         return 0;
1316 }
1317
1318 int ttsd_engine_setting_get_voice_list(char** engine_id, GList** voice_list)
1319 {
1320         if (false == g_agent_init) {
1321                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1322                 return TTSD_ERROR_OPERATION_FAILED;
1323         }
1324
1325         if (false == g_cur_engine.is_loaded) {
1326                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1327                 return TTSD_ERROR_OPERATION_FAILED;
1328         }
1329
1330         int ret = 0;
1331
1332         /* get language list from engine*/
1333         ret = ttsd_engine_get_voice_list(voice_list);
1334         if (0 != ret) {
1335                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail ttsd_engine_get_voice_list() ");
1336                 return ret;
1337         }
1338
1339         *engine_id = strdup(g_cur_engine.engine_uuid);
1340         
1341         return 0;
1342 }
1343
1344 int ttsd_engine_setting_get_default_voice(char** language, ttsp_voice_type_e* vctype)
1345 {
1346         if (false == g_agent_init) {
1347                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1348                 return TTSD_ERROR_OPERATION_FAILED;
1349         }
1350
1351         if (false == g_cur_engine.is_loaded) {
1352                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1353                 return TTSD_ERROR_OPERATION_FAILED;
1354         }
1355
1356         /* get current language from engine*/
1357         int ret = 0;
1358         char* temp_lang;
1359         ttsp_voice_type_e temp_int;
1360
1361         ret = ttsd_engine_get_default_voice(&temp_lang, &temp_int);
1362         if (0 != ret) {
1363                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail ttsd_engine_get_default_voice()");
1364                 return ret;
1365         } 
1366
1367         if (NULL != temp_lang) {
1368                 *language = strdup(temp_lang);
1369                 *vctype = temp_int;
1370                 free(temp_lang);
1371         } else {
1372                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail to get default language");
1373                 return TTSD_ERROR_OPERATION_FAILED;
1374         }
1375
1376         return 0;
1377 }
1378
1379 int ttsd_engine_setting_set_default_voice(const char* language, ttsp_voice_type_e vctype)
1380 {
1381         if (false == g_agent_init) {
1382                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1383                 return TTSD_ERROR_OPERATION_FAILED;
1384         }
1385
1386         if (false == g_cur_engine.is_loaded) {
1387                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1388                 return TTSD_ERROR_OPERATION_FAILED;
1389         }
1390
1391         if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
1392                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
1393                 return TTSD_ERROR_OPERATION_FAILED;
1394         }
1395
1396         int ret = -1;
1397         if(false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
1398                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
1399                 return TTSD_ERROR_INVALID_VOICE;
1400         }
1401
1402         if (NULL != g_cur_engine.default_lang)
1403                 free(g_cur_engine.default_lang);
1404
1405         g_cur_engine.default_lang = strdup(language);
1406         g_cur_engine.default_vctype = vctype;
1407
1408         ret = ttsd_config_set_default_voice(language, (int)vctype);
1409         if (0 == ret) {
1410                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
1411                                 g_cur_engine.default_lang, g_cur_engine.default_vctype); 
1412         } else {
1413                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to write default voice to config (%d)", ret); 
1414         }
1415
1416         return 0;
1417 }
1418
1419 int ttsd_engine_setting_get_default_speed(ttsp_speed_e* speed)
1420 {
1421         if (false == g_agent_init) {
1422                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1423                 return TTSD_ERROR_OPERATION_FAILED;
1424         }
1425
1426         if (false == g_cur_engine.is_loaded) {
1427                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1428                 return TTSD_ERROR_OPERATION_FAILED;
1429         }
1430         
1431         /* get current language */
1432         *speed = g_cur_engine.default_speed;
1433
1434         return 0;
1435 }
1436
1437 int ttsd_engine_setting_set_default_speed(const ttsp_speed_e speed)
1438 {
1439         if (false == g_agent_init) {
1440                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1441                 return TTSD_ERROR_OPERATION_FAILED;
1442         }
1443
1444         if (false == g_cur_engine.is_loaded) {
1445                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1446                 return TTSD_ERROR_OPERATION_FAILED;
1447         }
1448
1449         g_cur_engine.default_speed = speed;
1450
1451         if (0 != ttsd_config_set_default_speed(speed)) {
1452                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set default speed to config");
1453         }
1454
1455         return 0;
1456 }
1457
1458 bool __engine_setting_cb(const char* key, const char* value, void* user_data)
1459 {
1460         GList** engine_setting_list = (GList**)user_data;
1461
1462         if (NULL == engine_setting_list || NULL == key || NULL == value) {
1463                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in engine setting callback!!!!");
1464                 return false;
1465         }
1466
1467         engine_setting_s* temp = g_malloc0(sizeof(engine_setting_s));
1468         temp->key = g_strdup(key);
1469         temp->value = g_strdup(value);
1470
1471         *engine_setting_list = g_list_append(*engine_setting_list, temp);
1472
1473         return true;
1474 }
1475
1476
1477 int ttsd_engine_setting_get_engine_setting_info(char** engine_id, GList** setting_list)
1478 {
1479         if (false == g_agent_init) {
1480                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1481                 return TTSD_ERROR_OPERATION_FAILED;
1482         }
1483
1484         if (false == g_cur_engine.is_loaded) {
1485                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1486                 return TTSD_ERROR_OPERATION_FAILED;
1487         }
1488
1489         if (NULL == setting_list) {
1490                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL");
1491                 return TTSD_ERROR_INVALID_PARAMETER;
1492         }
1493
1494         if (NULL == g_cur_engine.pefuncs->foreach_engine_setting) {
1495                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] foreach_engine_setting() of engine is NULL!!");
1496                 return TTSD_ERROR_OPERATION_FAILED;
1497         }
1498
1499         /* get setting info and move setting info to input parameter */
1500         int result = 0;
1501
1502         result = g_cur_engine.pefuncs->foreach_engine_setting(__engine_setting_cb, setting_list);
1503
1504         if (0 == result && 0 < g_list_length(*setting_list)) {
1505                 *engine_id = strdup(g_cur_engine.engine_uuid);
1506         } else {
1507                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get setting info : result(%d)", result);
1508                 result = TTSD_ERROR_OPERATION_FAILED;
1509         }
1510
1511         return result;
1512 }
1513
1514 int ttsd_engine_setting_set_engine_setting(const char* key, const char* value)
1515 {
1516         if (false == g_agent_init) {
1517                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1518                 return TTSD_ERROR_OPERATION_FAILED;
1519         }
1520
1521         if (false == g_cur_engine.is_loaded) {
1522                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1523                 return TTSD_ERROR_OPERATION_FAILED;
1524         }
1525
1526         if (NULL == g_cur_engine.pefuncs->set_engine_setting) {
1527                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] set_setting_info of engine is NULL!!");
1528                 return TTSD_ERROR_OPERATION_FAILED;
1529         }
1530
1531         /* get setting info and move setting info to input parameter */
1532         int ret = 0;
1533         ret = g_cur_engine.pefuncs->set_engine_setting(key, value);
1534
1535         if (0 != ret) {
1536                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set engine setting : result(%d)", ret); 
1537                 return TTSD_ERROR_OPERATION_FAILED;
1538         } 
1539
1540         return 0;
1541 }
1542
1543 void __free_voice_list(GList* voice_list)
1544 {
1545         GList *iter = NULL;
1546         voice_s* data = NULL;
1547
1548         /* if list have item */
1549         if (g_list_length(voice_list) > 0) {
1550                 /* Get a first item */
1551                 iter = g_list_first(voice_list);
1552
1553                 while (NULL != iter) {
1554                         data = iter->data;
1555                         
1556                         if (NULL != data) {
1557                                 if (NULL != data->language)
1558                                         g_free(data->language);
1559
1560                                 g_free(data);
1561                         }
1562                         
1563                         voice_list = g_list_remove_link(voice_list, iter);
1564                         
1565                         iter = g_list_first(voice_list);
1566                 }
1567         }
1568 }
1569
1570 /*
1571 * TTS Engine Callback Functions                                                                                 `                                 *
1572 */
1573 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data)
1574 {
1575         g_result_cb(event, data, data_size, user_data);
1576
1577         return true;
1578 }
1579
1580 /* function for debugging */
1581 int ttsd_print_enginelist()
1582 {
1583         GList *iter = NULL;
1584         ttsengine_info_s *data = NULL;
1585
1586         if (g_list_length(g_engine_list) > 0) {
1587                 iter = g_list_first(g_engine_list);
1588
1589                 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1590
1591                 int i = 1;      
1592                 while (NULL != iter) {
1593                         data = iter->data;
1594
1595                         SLOG(LOG_DEBUG, get_tag(), "[%dth]", i);
1596                         SLOG(LOG_DEBUG, get_tag(), "engine uuid : %s", data->engine_uuid);
1597                         SLOG(LOG_DEBUG, get_tag(), "engine name : %s", data->engine_name);
1598                         SLOG(LOG_DEBUG, get_tag(), "engine path : %s", data->engine_path);
1599                         SLOG(LOG_DEBUG, get_tag(), "setting ug path : %s", data->setting_ug_path);
1600
1601                         iter = g_list_next(iter);
1602                         i++;
1603                 }
1604                 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1605                 SLOG(LOG_DEBUG, get_tag(), "  ");
1606         } else {
1607                 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1608                 SLOG(LOG_DEBUG, get_tag(), "No Engine in directory");
1609                 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1610         }
1611
1612         return 0;
1613 }
1614
1615
1616
1617
1618
1619
1620