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.
19 #include "ttsd_main.h"
20 #include "ttsd_engine_agent.h"
21 #include "ttsd_config.h"
23 #define ENGINE_PATH_SIZE 256
26 * Internal data structure
34 /* info for using engine load */
40 /* engine base setting */
46 ttspe_funcs_s* pefuncs;
47 ttspd_funcs_s* pdfuncs;
49 int (*ttsp_load_engine)(const ttspd_funcs_s* pdfuncs, ttspe_funcs_s* pefuncs);
50 int (*ttsp_unload_engine)();
57 char* setting_ug_path;
72 static bool g_agent_init;
74 /** TTS engine list */
75 static GList *g_engine_list;
77 /** Current engine information */
78 static ttsengine_s g_cur_engine;
80 /** Current voice information */
81 static GSList* g_cur_voices;
83 /** Result callback function */
84 static synth_result_callback g_result_cb;
87 /** Set current engine */
88 static int __internal_set_current_engine(const char* engine_uuid);
90 /** Check engine id */
91 static int __internal_check_engine_id(const char* engine_uuid);
93 /** Update engine list */
94 static int __internal_update_engine_list();
96 /** Get engine info */
97 static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info);
99 /** Callback function for result */
100 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size,
101 ttsp_audio_type_e audio_type, int rate, void *user_data);
103 /** Callback function for voice list */
104 static bool __supported_voice_cb(const char* language, int type, void* user_data);
106 /** Free voice list */
107 static void __free_voice_list(GList* voice_list);
109 /** Callback function for engine info */
110 static void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
111 bool use_network, void* user_data);
115 static int ttsd_print_enginelist();
117 static int ttsd_print_voicelist();
119 static const char* __ttsd_get_engine_error_code(ttsp_error_e err)
122 case TTSP_ERROR_NONE: return "TTSP_ERROR_NONE";
123 case TTSP_ERROR_OUT_OF_MEMORY: return "TTSP_ERROR_OUT_OF_MEMORY";
124 case TTSP_ERROR_IO_ERROR: return "TTSP_ERROR_IO_ERROR";
125 case TTSP_ERROR_INVALID_PARAMETER: return "TTSP_ERROR_INVALID_PARAMETER";
126 case TTSP_ERROR_OUT_OF_NETWORK: return "TTSP_ERROR_OUT_OF_NETWORK";
127 case TTSP_ERROR_INVALID_STATE: return "TTSP_ERROR_INVALID_STATE";
128 case TTSP_ERROR_INVALID_VOICE: return "TTSP_ERROR_INVALID_VOICE";
129 case TTSP_ERROR_OPERATION_FAILED: return "TTSP_ERROR_OPERATION_FAILED";
131 return "Invalid error code";
135 int ttsd_engine_agent_init(synth_result_callback result_cb)
137 /* initialize static data */
138 if (result_cb == NULL) {
139 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] invalid parameter");
140 return TTSD_ERROR_INVALID_PARAMETER;
143 g_result_cb = result_cb;
145 g_cur_engine.engine_uuid = NULL;
146 g_cur_engine.engine_name = NULL;
147 g_cur_engine.engine_path = NULL;
149 g_cur_engine.is_set = false;
150 g_cur_engine.handle = NULL;
151 g_cur_engine.pefuncs = (ttspe_funcs_s*)calloc(1, sizeof(ttspe_funcs_s));
152 if (NULL == g_cur_engine.pefuncs) {
153 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
154 return TTSP_ERROR_OUT_OF_MEMORY;
157 g_cur_engine.pdfuncs = (ttspd_funcs_s*)calloc(1, sizeof(ttspd_funcs_s));
158 if (NULL == g_cur_engine.pdfuncs) {
159 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
160 free(g_cur_engine.pefuncs);
161 g_cur_engine.pefuncs = NULL;
162 return TTSP_ERROR_OUT_OF_MEMORY;
167 if (0 != ttsd_config_get_default_voice(&(g_cur_engine.default_lang), &(g_cur_engine.default_vctype))) {
168 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default voice in config");
169 /* Set default voice */
170 g_cur_engine.default_lang = strdup(TTS_BASE_LANGUAGE);
171 g_cur_engine.default_vctype = TTSP_VOICE_TYPE_FEMALE;
174 if (0 != ttsd_config_get_default_speed(&(g_cur_engine.default_speed))) {
175 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default speed in config");
176 g_cur_engine.default_speed = TTS_SPEED_NORMAL;
179 if (0 != ttsd_config_get_default_pitch(&(g_cur_engine.default_pitch))) {
180 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default pitch in config");
181 g_cur_engine.default_pitch = TTS_PITCH_NORMAL;
184 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Initialize Engine Agent");
189 int ttsd_engine_agent_release()
191 if (false == g_agent_init) {
192 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
193 return TTSD_ERROR_OPERATION_FAILED;
196 /* unload current engine */
197 ttsd_engine_agent_unload_current_engine();
199 /* release engine list */
201 ttsengine_info_s *data = NULL;
203 if (g_list_length(g_engine_list) > 0) {
204 /* Get a first item */
205 iter = g_list_first(g_engine_list);
206 while (NULL != iter) {
207 /* Get data from item */
209 iter = g_list_remove(iter, data);
212 if (NULL != data->engine_uuid) free(data->engine_uuid);
213 if (NULL != data->engine_name) free(data->engine_name);
214 if (NULL != data->setting_ug_path) free(data->setting_ug_path);
215 if (NULL != data->engine_path) free(data->engine_path);
222 /* release current engine data */
223 if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
224 if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
225 if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
227 if (g_cur_engine.pefuncs != NULL) free(g_cur_engine.pefuncs);
228 if (g_cur_engine.pdfuncs != NULL) free(g_cur_engine.pdfuncs);
229 if (g_cur_engine.default_lang != NULL) free(g_cur_engine.default_lang);
231 g_agent_init = false;
233 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Release Engine Agent");
238 int ttsd_engine_agent_initialize_current_engine()
240 if (false == g_agent_init) {
241 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
242 return TTSD_ERROR_OPERATION_FAILED;
245 /* update engine list */
246 if (0 != __internal_update_engine_list()) {
247 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] No engine error");
248 return TTSD_ERROR_OPERATION_FAILED;
251 /* 2. get current engine from config */
252 char* cur_engine_uuid = NULL;
253 bool is_get_engineid_from_config = false;
255 if (0 != ttsd_config_get_default_engine(&cur_engine_uuid)) {
256 /*not set current engine */
257 /*set system default engine*/
259 ttsengine_info_s *data = NULL;
261 if (g_list_length(g_engine_list) > 0) {
262 iter = g_list_first(g_engine_list);
266 if (NULL != data->engine_uuid) {
267 cur_engine_uuid = strdup(data->engine_uuid);
268 ttsd_config_set_default_engine(cur_engine_uuid);
270 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Data of current engine is corrupt");
271 return TTSD_ERROR_OPERATION_FAILED;
275 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to set a engine of engine list");
276 return TTSD_ERROR_OPERATION_FAILED;
279 is_get_engineid_from_config = false;
281 is_get_engineid_from_config = true;
284 if (NULL == cur_engine_uuid) {
285 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
286 return TTSD_ERROR_OPERATION_FAILED;
289 /* check whether cur engine uuid is valid or not. */
290 if (0 != __internal_check_engine_id(cur_engine_uuid)) {
291 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] It is not valid engine id from config");
295 if (g_list_length(g_engine_list) > 0)
296 iter = g_list_first(g_engine_list);
298 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] NO TTS Engine !!");
299 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
300 return TTSD_ERROR_OPERATION_FAILED;
303 if (cur_engine_uuid != NULL) free(cur_engine_uuid);
304 ttsengine_info_s *data = NULL;
307 cur_engine_uuid = strdup(data->engine_uuid);
309 is_get_engineid_from_config = false;
312 if (NULL != cur_engine_uuid)
313 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
315 return TTSD_ERROR_OPERATION_FAILED;
317 /* set current engine */
318 if (0 != __internal_set_current_engine(cur_engine_uuid)) {
319 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
320 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
321 return TTSD_ERROR_OPERATION_FAILED;
324 if (false == is_get_engineid_from_config) {
325 if (0 != ttsd_config_set_default_engine(cur_engine_uuid)) {
326 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config");
330 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
332 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine");
337 static int __internal_check_engine_id(const char* engine_uuid)
340 ttsengine_s *data = NULL;
342 if (g_list_length(g_engine_list) > 0) {
343 iter = g_list_first(g_engine_list);
345 while (NULL != iter) {
348 if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid)))
351 iter = g_list_next(iter);
358 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
359 bool use_network, void* user_data)
361 ttsengine_info_s* temp = (ttsengine_info_s*)user_data;
363 if (NULL != engine_uuid)
364 temp->engine_uuid = strdup(engine_uuid);
366 if (NULL != engine_name)
367 temp->engine_name = strdup(engine_name);
369 if (NULL != setting_ug_name)
370 temp->setting_ug_path = strdup(setting_ug_name);
372 temp->use_network = use_network;
376 static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
381 handle = dlopen(filepath, RTLD_LAZY);
384 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath);
385 return TTSD_ERROR_OPERATION_FAILED;
388 /* link engine to daemon */
389 dlsym(handle, "ttsp_load_engine");
390 if ((error = dlerror()) != NULL) {
391 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_load_engine : path(%s) message(%s)", filepath, error);
393 return TTSD_ERROR_OPERATION_FAILED;
396 dlsym(handle, "ttsp_unload_engine");
397 if ((error = dlerror()) != NULL) {
398 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_unload_engine : path(%s) message(%s)", filepath, error);
400 return TTSD_ERROR_OPERATION_FAILED;
403 int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
405 get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
406 if (NULL != (error = dlerror()) || NULL == get_engine_info) {
407 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_get_engine_info() :path(%s) message(%s)", filepath, error);
409 return TTSD_ERROR_OPERATION_FAILED;
412 ttsengine_info_s* temp;
413 temp = (ttsengine_info_s*)calloc(1, sizeof(ttsengine_info_s));
415 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to alloc memory");
417 return TTSD_ERROR_OUT_OF_MEMORY;
420 /* get engine info */
421 if (0 != get_engine_info(&__engine_info_cb, (void*)temp)) {
422 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get engine info");
425 return TTSD_ERROR_OPERATION_FAILED;
431 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && true == temp->use_network) {
432 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent WARNING] %s is invalid because of network based", temp->engine_name);
433 if (NULL != temp->engine_uuid) free(temp->engine_uuid);
434 if (NULL != temp->engine_name) free(temp->engine_name);
435 if (NULL != temp->setting_ug_path) free(temp->setting_ug_path);
437 return TTSD_ERROR_OPERATION_FAILED;
440 temp->engine_path = strdup(filepath);
442 SLOG(LOG_DEBUG, get_tag(), "----- Valid engine");
443 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine uuid : %s", temp->engine_uuid);
444 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine name : %s", temp->engine_name);
445 SECURE_SLOG(LOG_DEBUG, get_tag(), "Setting path : %s", temp->setting_ug_path);
446 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine path : %s", temp->engine_path);
447 SECURE_SLOG(LOG_DEBUG, get_tag(), "Use network : %s", temp->use_network ? "true" : "false");
448 SLOG(LOG_DEBUG, get_tag(), "-----");
449 SLOG(LOG_DEBUG, get_tag(), " ");
456 static int __internal_update_engine_list()
458 /* relsease engine list */
460 ttsengine_info_s *data = NULL;
462 if (g_list_length(g_engine_list) > 0) {
463 iter = g_list_first(g_engine_list);
465 while (NULL != iter) {
468 if (data != NULL) free(data);
469 g_engine_list = g_list_remove_link(g_engine_list, iter);
471 iter = g_list_first(g_engine_list);
475 /* get file name from engine directory and get engine information from each filename */
479 struct dirent *dirp = NULL;
480 dp = opendir(TTS_DEFAULT_ENGINE);
484 ret = readdir_r(dp, &entry, &dirp);
486 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to read directory");
491 ttsengine_info_s* info;
492 char* filepath = NULL;
495 file_size = strlen(TTS_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
496 filepath = (char*)calloc(file_size, sizeof(char));
498 if (NULL != filepath) {
499 snprintf(filepath, file_size, "%s/%s", TTS_DEFAULT_ENGINE, dirp->d_name);
501 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!");
505 /* get its info and update engine list */
506 if (0 == __internal_get_engine_info(filepath, &info)) {
507 /* add engine info to g_engine_list */
508 g_engine_list = g_list_append(g_engine_list, info);
511 if (NULL != filepath) free(filepath);
513 } while (NULL != dirp);
518 if (g_list_length(g_engine_list) <= 0) {
519 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Engine");
520 return TTSD_ERROR_OPERATION_FAILED;
523 #ifdef ENGINE_AGENT_DEBUG
524 ttsd_print_enginelist();
529 static int __internal_set_current_engine(const char* engine_uuid)
531 /* check whether engine id is valid or not. */
533 ttsengine_info_s *data = NULL;
536 if (g_list_length(g_engine_list) > 0) {
537 iter = g_list_first(g_engine_list);
539 while (NULL != iter) {
542 if (0 == strncmp(data->engine_uuid, engine_uuid, strlen(engine_uuid))) {
548 iter = g_list_next(iter);
552 /* If current engine does not exist, return error */
554 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Cannot find engine id");
555 return TTSD_ERROR_OPERATION_FAILED;
557 if (g_cur_engine.engine_uuid != NULL) {
558 /*compare current engine uuid */
559 if (0 == strncmp(g_cur_engine.engine_uuid, data->engine_uuid, strlen(engine_uuid))) {
560 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] tts engine has already been set");
566 /* set data from g_engine_list */
567 if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
568 if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
569 if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
571 if (NULL == data->engine_uuid || NULL == data->engine_name || NULL == data->engine_path) {
572 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Engine data is NULL");
573 return TTSD_ERROR_OPERATION_FAILED;
576 g_cur_engine.engine_uuid = strdup(data->engine_uuid);
577 g_cur_engine.engine_name = strdup(data->engine_name);
578 g_cur_engine.engine_path = strdup(data->engine_path);
580 g_cur_engine.handle = NULL;
581 g_cur_engine.is_loaded = false;
582 g_cur_engine.is_set = true;
583 g_cur_engine.need_network = data->use_network;
585 SLOG(LOG_DEBUG, get_tag(), "-----");
586 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine uuid : %s", g_cur_engine.engine_uuid);
587 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine name : %s", g_cur_engine.engine_name);
588 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine path : %s", g_cur_engine.engine_path);
589 SLOG(LOG_DEBUG, get_tag(), "-----");
594 int __ttsd_get_mode(ttsp_mode_e* mode)
597 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
598 return TTSP_ERROR_INVALID_PARAMETER;
601 switch (ttsd_get_mode()) {
602 case TTSD_MODE_DEFAULT: *mode = TTSP_MODE_DEFAULT; break;
603 case TTSD_MODE_NOTIFICATION: *mode = TTSP_MODE_NOTIFICATION; break;
604 case TTSD_MODE_SCREEN_READER: *mode = TTSP_MODE_SCREEN_READER; break;
606 SLOG(LOG_ERROR, get_tag(), "[ERROR] tts mode is NOT valid");
612 int __ttsd_engine_agent_get_speed_range(int* min, int* normal, int* max)
614 if (NULL == min || NULL == normal || NULL == max) {
615 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
616 return TTSP_ERROR_INVALID_PARAMETER;
619 *min = TTS_SPEED_MIN;
620 *normal = TTS_SPEED_NORMAL;
621 *max = TTS_SPEED_MAX;
626 int __ttsd_engine_agent_get_pitch_range(int* min, int* normal, int* max)
628 if (NULL == min || NULL == normal || NULL == max) {
629 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
630 return TTSP_ERROR_INVALID_PARAMETER;
633 *min = TTS_PITCH_MIN;
634 *normal = TTS_PITCH_NORMAL;
635 *max = TTS_PITCH_MAX;
640 static bool __set_voice_info_cb(const char* language, int type, void* user_data)
642 if (NULL == language) {
643 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
647 ttsvoice_s* voice = calloc(1, sizeof(ttsvoice_s));
649 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
652 voice->lang = strdup(language);
655 voice->client_ref_count = 0;
656 voice->is_loaded = false;
658 if (0 == strcmp(g_cur_engine.default_lang, language) && g_cur_engine.default_vctype == type) {
659 voice->is_default = true;
660 voice->is_loaded = true;
662 voice->is_default = false;
665 g_cur_voices = g_slist_append(g_cur_voices, voice);
670 static int __update_voice_list()
676 ret = g_cur_engine.pefuncs->foreach_voices(__set_voice_info_cb, NULL);
677 if (0 != ret || 0 >= g_slist_length(g_cur_voices)) {
678 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
682 #ifdef ENGINE_AGENT_DEBUG
683 ttsd_print_voicelist();
688 int ttsd_engine_agent_load_current_engine()
690 if (false == g_agent_init) {
691 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
692 return TTSD_ERROR_OPERATION_FAILED;
695 if (false == g_cur_engine.is_set) {
696 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
697 return TTSD_ERROR_OPERATION_FAILED;
700 /* check whether current engine is loaded or not */
701 if (true == g_cur_engine.is_loaded) {
702 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Engine has already been loaded ");
708 g_cur_engine.handle = dlopen(g_cur_engine.engine_path, RTLD_LAZY); /* RTLD_LAZY RTLD_NOW*/
710 if (NULL != (error = dlerror()) || NULL == g_cur_engine.handle) {
711 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get current engine handle : %s", error);
715 g_cur_engine.ttsp_unload_engine = (int (*)())dlsym(g_cur_engine.handle, "ttsp_unload_engine");
716 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_unload_engine) {
717 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_unload_engine() : %s", error);
721 g_cur_engine.ttsp_load_engine = (int (*)(const ttspd_funcs_s* , ttspe_funcs_s*))dlsym(g_cur_engine.handle, "ttsp_load_engine");
722 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_load_engine) {
723 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_load_engine() : %s", error);
728 g_cur_engine.pdfuncs->version = 1;
729 g_cur_engine.pdfuncs->size = sizeof(ttspd_funcs_s);
730 g_cur_engine.pdfuncs->get_mode = __ttsd_get_mode;
731 g_cur_engine.pdfuncs->get_speed_range = __ttsd_engine_agent_get_speed_range;
732 g_cur_engine.pdfuncs->get_pitch_range = __ttsd_engine_agent_get_pitch_range;
735 ret = g_cur_engine.ttsp_load_engine(g_cur_engine.pdfuncs, g_cur_engine.pefuncs);
737 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to load engine - %s : result(%s)",
738 g_cur_engine.engine_path, __ttsd_get_engine_error_code(ret));
739 return TTSD_ERROR_OPERATION_FAILED;
742 /* engine error check */
743 if (g_cur_engine.pefuncs->size != sizeof(ttspe_funcs_s)) {
744 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] The size of engine function is not valid");
747 if (NULL == g_cur_engine.pefuncs->initialize ||
748 NULL == g_cur_engine.pefuncs->deinitialize ||
749 NULL == g_cur_engine.pefuncs->foreach_voices ||
750 NULL == g_cur_engine.pefuncs->is_valid_voice ||
751 NULL == g_cur_engine.pefuncs->start_synth ||
752 NULL == g_cur_engine.pefuncs->cancel_synth) {
753 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] The engine functions are NOT valid");
754 return TTSD_ERROR_OPERATION_FAILED;
757 ret = g_cur_engine.pefuncs->initialize(__result_cb);
759 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to initialize current engine : %s", __ttsd_get_engine_error_code(ret));
760 return TTSD_ERROR_OPERATION_FAILED;
763 /* Get voice info of current engine */
764 ret = __update_voice_list();
766 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set voice info : result(%d)", ret);
767 return TTSD_ERROR_OPERATION_FAILED;
770 /* Select default voice */
771 if (NULL != g_cur_engine.default_lang) {
772 if (true == g_cur_engine.pefuncs->is_valid_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype)) {
773 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set origin default voice to current engine : lang(%s), type(%d)",
774 g_cur_engine.default_lang, g_cur_engine.default_vctype);
776 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail set origin default voice : lang(%s), type(%d)",
777 g_cur_engine.default_lang, g_cur_engine.default_vctype);
779 return TTSD_ERROR_OPERATION_FAILED;
783 /* load default voice */
784 if (NULL != g_cur_engine.pefuncs->load_voice) {
785 ret = g_cur_engine.pefuncs->load_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype);
787 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load default voice : lang(%s), type(%d)",
788 g_cur_engine.default_lang, g_cur_engine.default_vctype);
790 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] Fail to load default voice : lang(%s), type(%d) result(%s)",
791 g_cur_engine.default_lang, g_cur_engine.default_vctype, __ttsd_get_engine_error_code(ret));
793 return TTSD_ERROR_OPERATION_FAILED;
797 /* set default pitch */
798 if (NULL != g_cur_engine.pefuncs->set_pitch) {
799 ret = g_cur_engine.pefuncs->set_pitch(g_cur_engine.default_pitch);
801 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
802 g_cur_engine.default_pitch, __ttsd_get_engine_error_code(ret));
803 return TTSD_ERROR_OPERATION_FAILED;
805 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default pitch : pitch(%d)", g_cur_engine.default_pitch);
808 g_cur_engine.is_loaded = true;
813 int ttsd_engine_agent_unload_current_engine()
815 if (false == g_agent_init) {
816 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
817 return TTSD_ERROR_OPERATION_FAILED;
820 if (false == g_cur_engine.is_set) {
821 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
822 return TTSD_ERROR_OPERATION_FAILED;
825 if (false == g_cur_engine.is_loaded) {
826 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Engine has already been unloaded ");
830 /* shutdown engine */
832 ret = g_cur_engine.pefuncs->deinitialize();
834 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Fail deinitialize() : %s", __ttsd_get_engine_error_code(ret));
838 g_cur_engine.ttsp_unload_engine();
840 dlclose(g_cur_engine.handle);
842 /* reset current engine data */
843 g_cur_engine.handle = NULL;
844 g_cur_engine.is_loaded = false;
847 ttsvoice_s* data = NULL;
849 iter = g_slist_nth(g_cur_voices, 0);
850 while (NULL != iter) {
854 if (NULL != data->lang) free(data->lang);
855 g_cur_voices = g_slist_remove(g_cur_voices, data);
860 iter = g_slist_nth(g_cur_voices, 0);
866 bool ttsd_engine_agent_need_network()
868 if (false == g_agent_init) {
869 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
870 return TTSD_ERROR_OPERATION_FAILED;
873 if (false == g_cur_engine.is_loaded) {
874 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
875 return TTSD_ERROR_OPERATION_FAILED;
878 return g_cur_engine.need_network;
881 bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang, int* out_type)
883 if (NULL == lang || NULL == out_lang || NULL == out_type) {
887 if (false == g_cur_engine.is_loaded) {
888 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Not loaded engine");
892 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Select voice : input lang(%s), input type(%d), default lang(%s), default type(%d)",
893 lang, type, g_cur_engine.default_lang, g_cur_engine.default_vctype);
895 /* case 1 : Both are default */
896 if (0 == strncmp(lang, "default", strlen("default")) && 0 == type) {
897 *out_lang = strdup(g_cur_engine.default_lang);
898 *out_type = g_cur_engine.default_vctype;
903 GList* voice_list = NULL;
906 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
907 if (0 != ret || 0 >= g_list_length(voice_list)) {
908 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
918 /* lang and type are not default type */
919 if (0 != strncmp(lang, "default", strlen("default")) && 0 != type) {
920 iter = g_list_first(voice_list);
922 while (NULL != iter) {
923 /* Get handle data from list */
926 if (0 == strncmp(voice->language, lang, strlen(lang)) && voice->type == type) {
927 *out_lang = strdup(voice->language);
928 *out_type = voice->type;
933 iter = g_list_next(iter);
936 } else if (0 != strncmp(lang, "default", strlen("default")) && 0 == type) {
937 /* Only type is default */
938 if (0 == strncmp(lang, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
939 *out_lang = strdup(g_cur_engine.default_lang);
940 *out_type = g_cur_engine.default_vctype;
943 voice_s* voice_selected = NULL;
944 iter = g_list_first(voice_list);
945 while (NULL != iter) {
946 /* Get handle data from list */
949 if (0 == strncmp(voice->language, lang, strlen(lang))) {
950 voice_selected = voice;
951 if (voice->type == g_cur_engine.default_vctype) {
952 voice_selected = voice;
956 iter = g_list_next(iter);
959 if (NULL != voice_selected) {
960 *out_lang = strdup(voice_selected->language);
961 *out_type = voice_selected->type;
965 } else if (0 == strncmp(lang, "default", strlen("default")) && 0 != type) {
966 /* Only lang is default */
967 if (type == g_cur_engine.default_vctype) {
968 *out_lang = strdup(g_cur_engine.default_lang);
969 *out_type = g_cur_engine.default_vctype;
972 voice_s* voice_selected = NULL;
973 iter = g_list_first(voice_list);
974 while (NULL != iter) {
975 /* Get handle data from list */
978 if (0 == strncmp(voice->language, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
979 voice_selected = voice;
980 if (voice->type == type) {
981 voice_selected = voice;
985 iter = g_list_next(iter);
988 if (NULL != voice_selected) {
989 *out_lang = strdup(voice->language);
990 *out_type = voice_selected->type;
996 if (true == result) {
997 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Selected voice : lang(%s), type(%d)", *out_lang, *out_type);
1000 __free_voice_list(voice_list);
1005 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
1007 if (false == g_agent_init) {
1008 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1012 if (false == g_cur_engine.is_loaded) {
1013 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1017 /* compare current engine and engine id.*/
1018 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1025 int ttsd_engine_agent_set_default_engine(const char* engine_id)
1027 if (false == g_agent_init) {
1028 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1029 return TTSD_ERROR_OPERATION_FAILED;
1032 /* compare current engine and new engine.*/
1033 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1034 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] new engine(%s) is the same as current engine", engine_id);
1038 bool is_engine_loaded = false;
1039 char* tmp_uuid = NULL;
1040 tmp_uuid = strdup(g_cur_engine.engine_uuid);
1041 if (NULL == tmp_uuid) {
1042 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
1043 return TTSD_ERROR_OPERATION_FAILED;
1046 is_engine_loaded = g_cur_engine.is_loaded;
1048 if (true == is_engine_loaded) {
1050 if (0 != ttsd_engine_agent_unload_current_engine()) {
1051 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to unload current engine");
1055 /* change current engine */
1056 if (0 != __internal_set_current_engine(engine_id)) {
1057 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set current engine. Recovery origin engine");
1059 /* roll back to old current engine. */
1060 __internal_set_current_engine(tmp_uuid);
1061 if (true == is_engine_loaded) {
1062 ttsd_engine_agent_load_current_engine();
1065 if (tmp_uuid != NULL) free(tmp_uuid);
1066 return TTSD_ERROR_OPERATION_FAILED;
1069 if (true == is_engine_loaded) {
1071 if (0 != ttsd_engine_agent_load_current_engine()) {
1072 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] Fail to load new engine. Recovery origin engine");
1074 /* roll back to old current engine. */
1075 __internal_set_current_engine(tmp_uuid);
1076 if (true == is_engine_loaded) {
1077 ttsd_engine_agent_load_current_engine();
1080 if (tmp_uuid != NULL) free(tmp_uuid);
1081 return TTSD_ERROR_OPERATION_FAILED;
1083 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load new engine : %s", engine_id);
1087 if (tmp_uuid != NULL) free(tmp_uuid);
1091 int ttsd_engine_agent_set_default_voice(const char* language, int vctype)
1093 if (false == g_agent_init) {
1094 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1095 return TTSD_ERROR_OPERATION_FAILED;
1098 if (false == g_cur_engine.is_loaded) {
1099 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1100 return TTSD_ERROR_OPERATION_FAILED;
1103 if (false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
1104 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
1105 return TTSD_ERROR_INVALID_VOICE;
1109 GSList *iter = NULL;
1110 ttsvoice_s* data = NULL;
1112 /* Update new default voice info */
1113 iter = g_slist_nth(g_cur_voices, 0);
1114 while (NULL != iter) {
1115 /* Get handle data from list */
1119 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1120 return TTSD_ERROR_OPERATION_FAILED;
1123 if (0 == strcmp(data->lang, language) && data->type == vctype) {
1124 data->is_default = true;
1125 if (0 == data->client_ref_count) {
1127 if (NULL != g_cur_engine.pefuncs->load_voice) {
1128 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1130 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load voice : lang(%s), type(%d)",
1131 data->lang, data->type);
1132 data->is_loaded = true;
1134 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1135 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1138 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1145 iter = g_slist_next(iter);
1148 #ifdef ENGINE_AGENT_DEBUG
1149 ttsd_print_voicelist();
1152 /* Update old default voice info */
1153 iter = g_slist_nth(g_cur_voices, 0);
1154 while (NULL != iter) {
1155 /*Get handle data from list*/
1159 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1160 return TTSD_ERROR_OPERATION_FAILED;
1163 if (0 == strcmp(data->lang, g_cur_engine.default_lang) && data->type == g_cur_engine.default_vctype) {
1164 data->is_default = false;
1165 if (0 == data->client_ref_count) {
1167 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1168 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1170 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1171 data->lang, data->type);
1172 data->is_loaded = false;
1174 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1175 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1178 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1185 iter = g_slist_next(iter);
1188 if (NULL != g_cur_engine.default_lang) free(g_cur_engine.default_lang);
1190 g_cur_engine.default_lang = strdup(language);
1191 g_cur_engine.default_vctype = vctype;
1193 #ifdef ENGINE_AGENT_DEBUG
1194 ttsd_print_voicelist();
1197 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
1198 g_cur_engine.default_lang, g_cur_engine.default_vctype);
1203 int ttsd_engine_agent_set_default_speed(int speed)
1205 if (false == g_agent_init) {
1206 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1207 return TTSD_ERROR_OPERATION_FAILED;
1210 g_cur_engine.default_speed = speed;
1215 int ttsd_engine_agent_set_default_pitch(int pitch)
1217 if (false == g_agent_init) {
1218 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1219 return TTSD_ERROR_OPERATION_FAILED;
1222 if (false == g_cur_engine.is_loaded) {
1223 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1224 return TTSD_ERROR_OPERATION_FAILED;
1227 if (NULL == g_cur_engine.pefuncs->set_pitch) {
1228 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not support pitch");
1229 return TTSD_ERROR_OPERATION_FAILED;
1232 int ret = g_cur_engine.pefuncs->set_pitch(pitch);
1234 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
1235 pitch, __ttsd_get_engine_error_code(ret));
1236 return TTSD_ERROR_OPERATION_FAILED;
1239 g_cur_engine.default_pitch = pitch;
1244 int ttsd_engine_agent_is_credential_needed(int uid, bool* credential_needed)
1246 if (NULL == credential_needed) {
1247 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid Parameter");
1248 return TTSP_ERROR_INVALID_PARAMETER;
1251 if (false == g_agent_init) {
1252 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1253 return TTSD_ERROR_OPERATION_FAILED;
1256 if (false == g_cur_engine.is_loaded) {
1257 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1258 return TTSD_ERROR_OPERATION_FAILED;
1261 if (NULL == g_cur_engine.pefuncs->need_app_credential) {
1262 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not support to check app credential");
1263 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1266 bool result = false;
1267 result = g_cur_engine.pefuncs->need_app_credential();
1268 *credential_needed = result;
1270 return TTSP_ERROR_NONE;
1273 /******************************************************************************************
1274 * TTS Engine Interfaces for client
1275 *******************************************************************************************/
1277 int ttsd_engine_load_voice(const char* lang, const int vctype)
1279 /* 1. Find voice info */
1281 GSList *iter = NULL;
1282 ttsvoice_s* data = NULL;
1284 iter = g_slist_nth(g_cur_voices, 0);
1285 while (NULL != iter) {
1286 /*Get handle data from list*/
1290 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1291 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1292 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1298 iter = g_slist_next(iter);
1303 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1304 return TTSD_ERROR_OPERATION_FAILED;
1307 /* 2. increse ref count */
1308 data->client_ref_count++;
1310 /* 3. if ref count change 0 to 1 and not default, load voice */
1311 if (1 == data->client_ref_count && false == data->is_default) {
1313 if (NULL != g_cur_engine.pefuncs->load_voice) {
1314 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1316 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load voice : lang(%s), type(%d)",
1317 data->lang, data->type);
1318 data->is_loaded = true;
1320 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1321 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1323 return TTSD_ERROR_OPERATION_FAILED;
1326 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1329 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not load voice : default voice(%d) or ref count(%d)",
1330 data->is_default, data->client_ref_count);
1333 #ifdef ENGINE_AGENT_DEBUG
1334 ttsd_print_voicelist();
1340 int ttsd_engine_unload_voice(const char* lang, const int vctype)
1342 /* 1. Find voice info */
1344 GSList *iter = NULL;
1345 ttsvoice_s* data = NULL;
1347 iter = g_slist_nth(g_cur_voices, 0);
1348 while (NULL != iter) {
1349 /*Get handle data from list*/
1353 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1354 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1355 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1361 iter = g_slist_next(iter);
1366 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1367 return TTSD_ERROR_OPERATION_FAILED;
1370 /* 2. Decrese ref count */
1371 data->client_ref_count--;
1373 /* 3. if ref count change 0 and not default, load voice */
1374 if (0 == data->client_ref_count && false == data->is_default) {
1376 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1377 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1379 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1380 data->lang, data->type);
1381 data->is_loaded = false;
1383 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1384 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1386 return TTSD_ERROR_OPERATION_FAILED;
1389 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1392 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not unload voice : default voice(%d) or ref count(%d)",
1393 data->is_default, data->client_ref_count);
1396 #ifdef ENGINE_AGENT_DEBUG
1397 ttsd_print_voicelist();
1402 int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text, int speed, const char* credential, void* user_param)
1404 if (false == g_agent_init) {
1405 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1406 return TTSD_ERROR_OPERATION_FAILED;
1409 if (false == g_cur_engine.is_loaded) {
1410 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1411 return TTSD_ERROR_OPERATION_FAILED;
1414 /* select voice for default */
1415 char* temp_lang = NULL;
1417 if (true != ttsd_engine_select_valid_voice(lang, vctype, &temp_lang, &temp_type)) {
1418 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to select default voice");
1419 if (NULL != temp_lang) free(temp_lang);
1420 return TTSD_ERROR_INVALID_VOICE;
1422 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Start synthesis : language(%s), type(%d), speed(%d), text(%s), credential(%s)",
1423 temp_lang, temp_type, speed, text, credential);
1426 if (NULL == g_cur_engine.pefuncs->start_synth) {
1427 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] start_synth() of engine is NULL!!");
1428 if (NULL != temp_lang) free(temp_lang);
1429 return TTSD_ERROR_OPERATION_FAILED;
1435 temp_speed = g_cur_engine.default_speed;
1440 /* synthesize text */
1442 ret = g_cur_engine.pefuncs->start_synth(temp_lang, temp_type, text, temp_speed, credential, user_param);
1444 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1445 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] * synthesize error : %s *", __ttsd_get_engine_error_code(ret));
1446 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1447 if (NULL != temp_lang) free(temp_lang);
1451 if (NULL != temp_lang) free(temp_lang);
1455 int ttsd_engine_cancel_synthesis()
1457 if (false == g_agent_init) {
1458 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1459 return TTSD_ERROR_OPERATION_FAILED;
1462 if (false == g_cur_engine.is_loaded) {
1463 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1464 return TTSD_ERROR_OPERATION_FAILED;
1467 if (NULL == g_cur_engine.pefuncs->cancel_synth) {
1468 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] cancel_synth() of engine is NULL!!");
1469 return TTSD_ERROR_OPERATION_FAILED;
1472 /* stop synthesis */
1474 ret = g_cur_engine.pefuncs->cancel_synth();
1476 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail cancel synthesis : %s", __ttsd_get_engine_error_code(ret));
1477 return TTSD_ERROR_OPERATION_FAILED;
1483 bool __supported_voice_cb(const char* language, int type, void* user_data)
1485 GList** voice_list = (GList**)user_data;
1487 if (NULL == language || NULL == voice_list) {
1488 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
1492 voice_s* voice = calloc(1, sizeof(voice_s));
1493 if (NULL == voice) {
1494 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
1497 voice->language = strdup(language);
1500 *voice_list = g_list_append(*voice_list, voice);
1505 int ttsd_engine_get_voice_list(GList** voice_list)
1507 if (false == g_agent_init) {
1508 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1509 return TTSD_ERROR_OPERATION_FAILED;
1512 if (false == g_cur_engine.is_loaded) {
1513 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1514 return TTSD_ERROR_OPERATION_FAILED;
1518 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, (void*)voice_list);
1520 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get voice list : %s", __ttsd_get_engine_error_code(ret));
1521 return TTSD_ERROR_OPERATION_FAILED;
1527 int ttsd_engine_get_default_voice(char** lang, int* vctype)
1529 if (false == g_agent_init) {
1530 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1531 return TTSD_ERROR_OPERATION_FAILED;
1534 if (false == g_cur_engine.is_loaded) {
1535 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1536 return TTSD_ERROR_OPERATION_FAILED;
1539 if (NULL == lang || NULL == vctype) {
1540 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] BAD Parameter");
1541 return TTSD_ERROR_INVALID_PARAMETER;
1544 if (NULL != g_cur_engine.default_lang) {
1545 *lang = strdup(g_cur_engine.default_lang);
1546 *vctype = g_cur_engine.default_vctype;
1548 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice : language(%s), type(%d)", *lang, *vctype);
1550 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Default voice is NULL");
1551 return TTSD_ERROR_OPERATION_FAILED;
1557 int ttsd_engine_set_private_data(const char* key, const char* data)
1559 if (false == g_agent_init) {
1560 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1561 return TTSD_ERROR_OPERATION_FAILED;
1564 if (false == g_cur_engine.is_loaded) {
1565 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No loaded engine");
1566 return TTSD_ERROR_ENGINE_NOT_FOUND;
1570 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid parameter");
1571 return TTSD_ERROR_INVALID_PARAMETER;
1574 if (NULL == g_cur_engine.pefuncs->set_private_data) {
1575 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not supported feature");
1576 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1580 ret = g_cur_engine.pefuncs->set_private_data(key, data);
1582 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set private data(%d)", ret);
1588 int ttsd_engine_get_private_data(const char* key, char** data)
1590 if (false == g_agent_init) {
1591 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1592 return TTSD_ERROR_OPERATION_FAILED;
1595 if (false == g_cur_engine.is_loaded) {
1596 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No loaded engine");
1597 return TTSD_ERROR_ENGINE_NOT_FOUND;
1600 if (NULL == key || NULL == data) {
1601 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid parameter");
1602 return TTSD_ERROR_INVALID_PARAMETER;
1605 if (NULL == g_cur_engine.pefuncs->get_private_data) {
1606 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not supported feature");
1607 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1612 ret = g_cur_engine.pefuncs->get_private_data(key, &temp);
1614 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get private data(%d)", ret);
1617 *data = strdup(temp);
1623 void __free_voice_list(GList* voice_list)
1626 voice_s* data = NULL;
1628 /* if list have item */
1629 if (g_list_length(voice_list) > 0) {
1630 /* Get a first item */
1631 iter = g_list_first(voice_list);
1633 while (NULL != iter) {
1637 if (NULL != data->language) free(data->language);
1641 voice_list = g_list_remove_link(voice_list, iter);
1643 iter = g_list_first(voice_list);
1649 * TTS Engine Callback Functions ` *
1651 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, ttsp_audio_type_e audio_type, int rate, void *user_data)
1653 g_result_cb(event, data, data_size, audio_type, rate, user_data);
1657 /* function for debugging */
1658 int ttsd_print_enginelist()
1661 ttsengine_info_s *data = NULL;
1663 if (g_list_length(g_engine_list) > 0) {
1664 iter = g_list_first(g_engine_list);
1666 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1669 while (NULL != iter) {
1672 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth]", i);
1673 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine uuid : %s", data->engine_uuid);
1674 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine name : %s", data->engine_name);
1675 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine path : %s", data->engine_path);
1676 SECURE_SLOG(LOG_DEBUG, get_tag(), "setting ug path : %s", data->setting_ug_path);
1678 iter = g_list_next(iter);
1681 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1682 SLOG(LOG_DEBUG, get_tag(), " ");
1684 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1685 SLOG(LOG_DEBUG, get_tag(), "No Engine in directory");
1686 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1692 int ttsd_print_voicelist()
1695 GSList *iter = NULL;
1696 ttsvoice_s* data = NULL;
1698 SLOG(LOG_DEBUG, get_tag(), "=== Voice list ===");
1700 if (g_slist_length(g_cur_voices) > 0) {
1701 /* Get a first item */
1702 iter = g_slist_nth(g_cur_voices, 0);
1705 while (NULL != iter) {
1706 /*Get handle data from list*/
1709 if (NULL == data || NULL == data->lang) {
1710 SLOG(LOG_ERROR, get_tag(), "[ERROR] Data is invalid");
1714 SLOG(LOG_DEBUG, get_tag(), "[%dth] default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1715 i, data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1718 iter = g_slist_next(iter);
1723 SLOG(LOG_DEBUG, get_tag(), "==================");