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) {
300 free(cur_engine_uuid);
301 cur_engine_uuid = NULL;
303 return TTSD_ERROR_OPERATION_FAILED;
307 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] No valid TTS Engine");
308 if (NULL != cur_engine_uuid) {
309 free(cur_engine_uuid);
310 cur_engine_uuid = NULL;
312 return TTSD_ERROR_OPERATION_FAILED;
315 if (cur_engine_uuid != NULL) {
316 free(cur_engine_uuid);
317 cur_engine_uuid = NULL;
319 ttsengine_info_s *data = NULL;
322 if (NULL == data || NULL == data->engine_uuid) {
323 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No valid TTS Engine");
324 return TTSD_ERROR_OPERATION_FAILED;
327 cur_engine_uuid = strdup(data->engine_uuid);
329 is_get_engineid_from_config = false;
332 if (NULL != cur_engine_uuid)
333 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
335 return TTSD_ERROR_OPERATION_FAILED;
337 /* set current engine */
338 if (0 != __internal_set_current_engine(cur_engine_uuid)) {
339 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
340 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
341 return TTSD_ERROR_OPERATION_FAILED;
344 if (false == is_get_engineid_from_config) {
345 if (0 != ttsd_config_set_default_engine(cur_engine_uuid)) {
346 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config");
350 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
352 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine");
357 static int __internal_check_engine_id(const char* engine_uuid)
360 ttsengine_s *data = NULL;
362 if (g_list_length(g_engine_list) > 0) {
363 iter = g_list_first(g_engine_list);
365 while (NULL != iter) {
368 if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid)))
371 iter = g_list_next(iter);
378 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
379 bool use_network, void* user_data)
381 ttsengine_info_s* temp = (ttsengine_info_s*)user_data;
383 if (NULL != engine_uuid)
384 temp->engine_uuid = strdup(engine_uuid);
386 if (NULL != engine_name)
387 temp->engine_name = strdup(engine_name);
389 if (NULL != setting_ug_name)
390 temp->setting_ug_path = strdup(setting_ug_name);
392 temp->use_network = use_network;
396 static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
401 handle = dlopen(filepath, RTLD_LAZY);
404 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath);
405 return TTSD_ERROR_OPERATION_FAILED;
408 /* link engine to daemon */
409 dlsym(handle, "ttsp_load_engine");
410 if ((error = dlerror()) != NULL) {
411 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_load_engine : path(%s) message(%s)", filepath, error);
413 return TTSD_ERROR_OPERATION_FAILED;
416 dlsym(handle, "ttsp_unload_engine");
417 if ((error = dlerror()) != NULL) {
418 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_unload_engine : path(%s) message(%s)", filepath, error);
420 return TTSD_ERROR_OPERATION_FAILED;
423 int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
425 get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
426 if (NULL != (error = dlerror()) || NULL == get_engine_info) {
427 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_get_engine_info() :path(%s) message(%s)", filepath, error);
429 return TTSD_ERROR_OPERATION_FAILED;
432 ttsengine_info_s* temp;
433 temp = (ttsengine_info_s*)calloc(1, sizeof(ttsengine_info_s));
435 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to alloc memory");
437 return TTSD_ERROR_OUT_OF_MEMORY;
440 /* get engine info */
441 if (0 != get_engine_info(&__engine_info_cb, (void*)temp)) {
442 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get engine info");
445 return TTSD_ERROR_OPERATION_FAILED;
451 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && true == temp->use_network) {
452 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent WARNING] %s is invalid because of network based", temp->engine_name);
453 if (NULL != temp->engine_uuid) free(temp->engine_uuid);
454 if (NULL != temp->engine_name) free(temp->engine_name);
455 if (NULL != temp->setting_ug_path) free(temp->setting_ug_path);
457 return TTSD_ERROR_OPERATION_FAILED;
460 temp->engine_path = strdup(filepath);
462 SLOG(LOG_DEBUG, get_tag(), "----- Valid engine");
463 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine uuid : %s", temp->engine_uuid);
464 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine name : %s", temp->engine_name);
465 SECURE_SLOG(LOG_DEBUG, get_tag(), "Setting path : %s", temp->setting_ug_path);
466 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine path : %s", temp->engine_path);
467 SECURE_SLOG(LOG_DEBUG, get_tag(), "Use network : %s", temp->use_network ? "true" : "false");
468 SLOG(LOG_DEBUG, get_tag(), "-----");
469 SLOG(LOG_DEBUG, get_tag(), " ");
476 static int __internal_update_engine_list()
478 /* relsease engine list */
480 ttsengine_info_s *data = NULL;
482 if (g_list_length(g_engine_list) > 0) {
483 iter = g_list_first(g_engine_list);
485 while (NULL != iter) {
488 if (data != NULL) free(data);
489 g_engine_list = g_list_remove_link(g_engine_list, iter);
491 iter = g_list_first(g_engine_list);
495 /* get file name from engine directory and get engine information from each filename */
499 struct dirent *dirp = NULL;
500 dp = opendir(TTS_DEFAULT_ENGINE);
504 ret = readdir_r(dp, &entry, &dirp);
506 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to read directory");
511 ttsengine_info_s* info;
512 char* filepath = NULL;
515 file_size = strlen(TTS_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
516 filepath = (char*)calloc(file_size, sizeof(char));
518 if (NULL != filepath) {
519 snprintf(filepath, file_size, "%s/%s", TTS_DEFAULT_ENGINE, dirp->d_name);
521 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!");
525 /* get its info and update engine list */
526 if (0 == __internal_get_engine_info(filepath, &info)) {
527 /* add engine info to g_engine_list */
528 g_engine_list = g_list_append(g_engine_list, info);
531 if (NULL != filepath) free(filepath);
533 } while (NULL != dirp);
538 if (g_list_length(g_engine_list) <= 0) {
539 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Engine");
540 return TTSD_ERROR_OPERATION_FAILED;
543 #ifdef ENGINE_AGENT_DEBUG
544 ttsd_print_enginelist();
549 static int __internal_set_current_engine(const char* engine_uuid)
551 /* check whether engine id is valid or not. */
553 ttsengine_info_s *data = NULL;
556 if (g_list_length(g_engine_list) > 0) {
557 iter = g_list_first(g_engine_list);
559 while (NULL != iter) {
562 if (0 == strncmp(data->engine_uuid, engine_uuid, strlen(engine_uuid))) {
568 iter = g_list_next(iter);
572 /* If current engine does not exist, return error */
574 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Cannot find engine id");
575 return TTSD_ERROR_OPERATION_FAILED;
577 if (g_cur_engine.engine_uuid != NULL) {
578 /*compare current engine uuid */
579 if (0 == strncmp(g_cur_engine.engine_uuid, data->engine_uuid, strlen(engine_uuid))) {
580 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] tts engine has already been set");
586 /* set data from g_engine_list */
587 if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
588 if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
589 if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
591 if (NULL == data->engine_uuid || NULL == data->engine_name || NULL == data->engine_path) {
592 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Engine data is NULL");
593 return TTSD_ERROR_OPERATION_FAILED;
596 g_cur_engine.engine_uuid = strdup(data->engine_uuid);
597 g_cur_engine.engine_name = strdup(data->engine_name);
598 g_cur_engine.engine_path = strdup(data->engine_path);
600 g_cur_engine.handle = NULL;
601 g_cur_engine.is_loaded = false;
602 g_cur_engine.is_set = true;
603 g_cur_engine.need_network = data->use_network;
605 SLOG(LOG_DEBUG, get_tag(), "-----");
606 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine uuid : %s", g_cur_engine.engine_uuid);
607 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine name : %s", g_cur_engine.engine_name);
608 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine path : %s", g_cur_engine.engine_path);
609 SLOG(LOG_DEBUG, get_tag(), "-----");
614 int __ttsd_get_mode(ttsp_mode_e* mode)
617 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
618 return TTSP_ERROR_INVALID_PARAMETER;
621 switch (ttsd_get_mode()) {
622 case TTSD_MODE_DEFAULT: *mode = TTSP_MODE_DEFAULT; break;
623 case TTSD_MODE_NOTIFICATION: *mode = TTSP_MODE_NOTIFICATION; break;
624 case TTSD_MODE_SCREEN_READER: *mode = TTSP_MODE_SCREEN_READER; break;
626 SLOG(LOG_ERROR, get_tag(), "[ERROR] tts mode is NOT valid");
632 int __ttsd_engine_agent_get_speed_range(int* min, int* normal, int* max)
634 if (NULL == min || NULL == normal || NULL == max) {
635 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
636 return TTSP_ERROR_INVALID_PARAMETER;
639 *min = TTS_SPEED_MIN;
640 *normal = TTS_SPEED_NORMAL;
641 *max = TTS_SPEED_MAX;
646 int __ttsd_engine_agent_get_pitch_range(int* min, int* normal, int* max)
648 if (NULL == min || NULL == normal || NULL == max) {
649 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
650 return TTSP_ERROR_INVALID_PARAMETER;
653 *min = TTS_PITCH_MIN;
654 *normal = TTS_PITCH_NORMAL;
655 *max = TTS_PITCH_MAX;
660 static bool __set_voice_info_cb(const char* language, int type, void* user_data)
662 if (NULL == language) {
663 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
667 ttsvoice_s* voice = calloc(1, sizeof(ttsvoice_s));
669 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
672 voice->lang = strdup(language);
675 voice->client_ref_count = 0;
676 voice->is_loaded = false;
678 if (0 == strcmp(g_cur_engine.default_lang, language) && g_cur_engine.default_vctype == type) {
679 voice->is_default = true;
680 voice->is_loaded = true;
682 voice->is_default = false;
685 g_cur_voices = g_slist_append(g_cur_voices, voice);
690 static int __update_voice_list()
696 ret = g_cur_engine.pefuncs->foreach_voices(__set_voice_info_cb, NULL);
697 if (0 != ret || 0 >= g_slist_length(g_cur_voices)) {
698 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
702 #ifdef ENGINE_AGENT_DEBUG
703 ttsd_print_voicelist();
708 int ttsd_engine_agent_load_current_engine()
710 if (false == g_agent_init) {
711 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
712 return TTSD_ERROR_OPERATION_FAILED;
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;
720 /* check whether current engine is loaded or not */
721 if (true == g_cur_engine.is_loaded) {
722 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Engine has already been loaded ");
728 g_cur_engine.handle = dlopen(g_cur_engine.engine_path, RTLD_LAZY); /* RTLD_LAZY RTLD_NOW*/
730 if (NULL != (error = dlerror()) || NULL == g_cur_engine.handle) {
731 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get current engine handle : %s", error);
735 g_cur_engine.ttsp_unload_engine = (int (*)())dlsym(g_cur_engine.handle, "ttsp_unload_engine");
736 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_unload_engine) {
737 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_unload_engine() : %s", error);
741 g_cur_engine.ttsp_load_engine = (int (*)(const ttspd_funcs_s* , ttspe_funcs_s*))dlsym(g_cur_engine.handle, "ttsp_load_engine");
742 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_load_engine) {
743 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_load_engine() : %s", error);
748 g_cur_engine.pdfuncs->version = 1;
749 g_cur_engine.pdfuncs->size = sizeof(ttspd_funcs_s);
750 g_cur_engine.pdfuncs->get_mode = __ttsd_get_mode;
751 g_cur_engine.pdfuncs->get_speed_range = __ttsd_engine_agent_get_speed_range;
752 g_cur_engine.pdfuncs->get_pitch_range = __ttsd_engine_agent_get_pitch_range;
755 ret = g_cur_engine.ttsp_load_engine(g_cur_engine.pdfuncs, g_cur_engine.pefuncs);
757 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to load engine - %s : result(%s)",
758 g_cur_engine.engine_path, __ttsd_get_engine_error_code(ret));
759 return TTSD_ERROR_OPERATION_FAILED;
762 /* engine error check */
763 if (g_cur_engine.pefuncs->size != sizeof(ttspe_funcs_s)) {
764 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] The size of engine function is not valid");
767 if (NULL == g_cur_engine.pefuncs->initialize ||
768 NULL == g_cur_engine.pefuncs->deinitialize ||
769 NULL == g_cur_engine.pefuncs->foreach_voices ||
770 NULL == g_cur_engine.pefuncs->is_valid_voice ||
771 NULL == g_cur_engine.pefuncs->start_synth ||
772 NULL == g_cur_engine.pefuncs->cancel_synth) {
773 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] The engine functions are NOT valid");
774 return TTSD_ERROR_OPERATION_FAILED;
777 ret = g_cur_engine.pefuncs->initialize(__result_cb);
779 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to initialize current engine : %s", __ttsd_get_engine_error_code(ret));
780 return TTSD_ERROR_OPERATION_FAILED;
783 /* Get voice info of current engine */
784 ret = __update_voice_list();
786 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set voice info : result(%d)", ret);
787 return TTSD_ERROR_OPERATION_FAILED;
790 /* Select default voice */
791 if (NULL != g_cur_engine.default_lang) {
792 if (true == g_cur_engine.pefuncs->is_valid_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype)) {
793 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set origin default voice to current engine : lang(%s), type(%d)",
794 g_cur_engine.default_lang, g_cur_engine.default_vctype);
796 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail set origin default voice : lang(%s), type(%d)",
797 g_cur_engine.default_lang, g_cur_engine.default_vctype);
799 return TTSD_ERROR_OPERATION_FAILED;
803 /* load default voice */
804 if (NULL != g_cur_engine.pefuncs->load_voice) {
805 ret = g_cur_engine.pefuncs->load_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype);
807 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load default voice : lang(%s), type(%d)",
808 g_cur_engine.default_lang, g_cur_engine.default_vctype);
810 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] Fail to load default voice : lang(%s), type(%d) result(%s)",
811 g_cur_engine.default_lang, g_cur_engine.default_vctype, __ttsd_get_engine_error_code(ret));
813 return TTSD_ERROR_OPERATION_FAILED;
817 /* set default pitch */
818 if (NULL != g_cur_engine.pefuncs->set_pitch) {
819 ret = g_cur_engine.pefuncs->set_pitch(g_cur_engine.default_pitch);
821 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
822 g_cur_engine.default_pitch, __ttsd_get_engine_error_code(ret));
823 return TTSD_ERROR_OPERATION_FAILED;
825 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default pitch : pitch(%d)", g_cur_engine.default_pitch);
828 g_cur_engine.is_loaded = true;
833 int ttsd_engine_agent_unload_current_engine()
835 if (false == g_agent_init) {
836 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
837 return TTSD_ERROR_OPERATION_FAILED;
840 if (false == g_cur_engine.is_set) {
841 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
842 return TTSD_ERROR_OPERATION_FAILED;
845 if (false == g_cur_engine.is_loaded) {
846 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Engine has already been unloaded ");
850 /* shutdown engine */
852 ret = g_cur_engine.pefuncs->deinitialize();
854 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Fail deinitialize() : %s", __ttsd_get_engine_error_code(ret));
858 g_cur_engine.ttsp_unload_engine();
860 dlclose(g_cur_engine.handle);
862 /* reset current engine data */
863 g_cur_engine.handle = NULL;
864 g_cur_engine.is_loaded = false;
867 ttsvoice_s* data = NULL;
869 iter = g_slist_nth(g_cur_voices, 0);
870 while (NULL != iter) {
874 if (NULL != data->lang) free(data->lang);
875 g_cur_voices = g_slist_remove(g_cur_voices, data);
880 iter = g_slist_nth(g_cur_voices, 0);
886 bool ttsd_engine_agent_need_network()
888 if (false == g_agent_init) {
889 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
890 return TTSD_ERROR_OPERATION_FAILED;
893 if (false == g_cur_engine.is_loaded) {
894 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
895 return TTSD_ERROR_OPERATION_FAILED;
898 return g_cur_engine.need_network;
901 bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang, int* out_type)
903 if (NULL == lang || NULL == out_lang || NULL == out_type) {
907 if (false == g_cur_engine.is_loaded) {
908 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Not loaded engine");
912 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Select voice : input lang(%s), input type(%d), default lang(%s), default type(%d)",
913 lang, type, g_cur_engine.default_lang, g_cur_engine.default_vctype);
915 /* case 1 : Both are default */
916 if (0 == strncmp(lang, "default", strlen("default")) && 0 == type) {
917 *out_lang = strdup(g_cur_engine.default_lang);
918 *out_type = g_cur_engine.default_vctype;
923 GList* voice_list = NULL;
926 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
927 if (0 != ret || 0 >= g_list_length(voice_list)) {
928 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
938 /* lang and type are not default type */
939 if (0 != strncmp(lang, "default", strlen("default")) && 0 != type) {
940 iter = g_list_first(voice_list);
942 while (NULL != iter) {
943 /* Get handle data from list */
946 if (0 == strncmp(voice->language, lang, strlen(lang)) && voice->type == type) {
947 *out_lang = strdup(voice->language);
948 *out_type = voice->type;
953 iter = g_list_next(iter);
956 } else if (0 != strncmp(lang, "default", strlen("default")) && 0 == type) {
957 /* Only type is default */
958 if (0 == strncmp(lang, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
959 *out_lang = strdup(g_cur_engine.default_lang);
960 *out_type = g_cur_engine.default_vctype;
963 voice_s* voice_selected = NULL;
964 iter = g_list_first(voice_list);
965 while (NULL != iter) {
966 /* Get handle data from list */
969 if (0 == strncmp(voice->language, lang, strlen(lang))) {
970 voice_selected = voice;
971 if (voice->type == g_cur_engine.default_vctype) {
972 voice_selected = voice;
976 iter = g_list_next(iter);
979 if (NULL != voice_selected) {
980 *out_lang = strdup(voice_selected->language);
981 *out_type = voice_selected->type;
985 } else if (0 == strncmp(lang, "default", strlen("default")) && 0 != type) {
986 /* Only lang is default */
987 if (type == g_cur_engine.default_vctype) {
988 *out_lang = strdup(g_cur_engine.default_lang);
989 *out_type = g_cur_engine.default_vctype;
992 voice_s* voice_selected = NULL;
993 iter = g_list_first(voice_list);
994 while (NULL != iter) {
995 /* Get handle data from list */
998 if (0 == strncmp(voice->language, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
999 voice_selected = voice;
1000 if (voice->type == type) {
1001 voice_selected = voice;
1005 iter = g_list_next(iter);
1008 if (NULL != voice_selected) {
1009 *out_lang = strdup(voice->language);
1010 *out_type = voice_selected->type;
1016 if (true == result) {
1017 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Selected voice : lang(%s), type(%d)", *out_lang, *out_type);
1020 __free_voice_list(voice_list);
1025 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
1027 if (false == g_agent_init) {
1028 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1032 if (false == g_cur_engine.is_loaded) {
1033 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1037 /* compare current engine and engine id.*/
1038 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1045 int ttsd_engine_agent_set_default_engine(const char* engine_id)
1047 if (false == g_agent_init) {
1048 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1049 return TTSD_ERROR_OPERATION_FAILED;
1052 /* compare current engine and new engine.*/
1053 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1054 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] new engine(%s) is the same as current engine", engine_id);
1058 bool is_engine_loaded = false;
1059 char* tmp_uuid = NULL;
1060 tmp_uuid = strdup(g_cur_engine.engine_uuid);
1061 if (NULL == tmp_uuid) {
1062 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
1063 return TTSD_ERROR_OPERATION_FAILED;
1066 is_engine_loaded = g_cur_engine.is_loaded;
1068 if (true == is_engine_loaded) {
1070 if (0 != ttsd_engine_agent_unload_current_engine()) {
1071 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to unload current engine");
1075 /* change current engine */
1076 if (0 != __internal_set_current_engine(engine_id)) {
1077 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set current engine. Recovery origin engine");
1079 /* roll back to old current engine. */
1080 __internal_set_current_engine(tmp_uuid);
1081 if (true == is_engine_loaded) {
1082 ttsd_engine_agent_load_current_engine();
1085 if (tmp_uuid != NULL) free(tmp_uuid);
1086 return TTSD_ERROR_OPERATION_FAILED;
1089 if (true == is_engine_loaded) {
1091 if (0 != ttsd_engine_agent_load_current_engine()) {
1092 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] Fail to load new engine. Recovery origin engine");
1094 /* roll back to old current engine. */
1095 __internal_set_current_engine(tmp_uuid);
1096 if (true == is_engine_loaded) {
1097 ttsd_engine_agent_load_current_engine();
1100 if (tmp_uuid != NULL) free(tmp_uuid);
1101 return TTSD_ERROR_OPERATION_FAILED;
1103 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load new engine : %s", engine_id);
1107 if (tmp_uuid != NULL) free(tmp_uuid);
1111 int ttsd_engine_agent_set_default_voice(const char* language, int vctype)
1113 if (false == g_agent_init) {
1114 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1115 return TTSD_ERROR_OPERATION_FAILED;
1118 if (false == g_cur_engine.is_loaded) {
1119 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1120 return TTSD_ERROR_OPERATION_FAILED;
1123 if (false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
1124 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
1125 return TTSD_ERROR_INVALID_VOICE;
1129 GSList *iter = NULL;
1130 ttsvoice_s* data = NULL;
1132 /* Update new default voice info */
1133 iter = g_slist_nth(g_cur_voices, 0);
1134 while (NULL != iter) {
1135 /* Get handle data from list */
1139 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1140 return TTSD_ERROR_OPERATION_FAILED;
1143 if (0 == strcmp(data->lang, language) && data->type == vctype) {
1144 data->is_default = true;
1145 if (0 == data->client_ref_count) {
1147 if (NULL != g_cur_engine.pefuncs->load_voice) {
1148 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1150 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load voice : lang(%s), type(%d)",
1151 data->lang, data->type);
1152 data->is_loaded = true;
1154 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1155 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1158 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1165 iter = g_slist_next(iter);
1168 #ifdef ENGINE_AGENT_DEBUG
1169 ttsd_print_voicelist();
1172 /* Update old default voice info */
1173 iter = g_slist_nth(g_cur_voices, 0);
1174 while (NULL != iter) {
1175 /*Get handle data from list*/
1179 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1180 return TTSD_ERROR_OPERATION_FAILED;
1183 if (0 == strcmp(data->lang, g_cur_engine.default_lang) && data->type == g_cur_engine.default_vctype) {
1184 data->is_default = false;
1185 if (0 == data->client_ref_count) {
1187 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1188 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1190 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1191 data->lang, data->type);
1192 data->is_loaded = false;
1194 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1195 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1198 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1205 iter = g_slist_next(iter);
1208 if (NULL != g_cur_engine.default_lang) free(g_cur_engine.default_lang);
1210 g_cur_engine.default_lang = strdup(language);
1211 g_cur_engine.default_vctype = vctype;
1213 #ifdef ENGINE_AGENT_DEBUG
1214 ttsd_print_voicelist();
1217 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
1218 g_cur_engine.default_lang, g_cur_engine.default_vctype);
1223 int ttsd_engine_agent_set_default_speed(int speed)
1225 if (false == g_agent_init) {
1226 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1227 return TTSD_ERROR_OPERATION_FAILED;
1230 g_cur_engine.default_speed = speed;
1235 int ttsd_engine_agent_set_default_pitch(int pitch)
1237 if (false == g_agent_init) {
1238 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1239 return TTSD_ERROR_OPERATION_FAILED;
1242 if (false == g_cur_engine.is_loaded) {
1243 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1244 return TTSD_ERROR_OPERATION_FAILED;
1247 if (NULL == g_cur_engine.pefuncs->set_pitch) {
1248 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not support pitch");
1249 return TTSD_ERROR_OPERATION_FAILED;
1252 int ret = g_cur_engine.pefuncs->set_pitch(pitch);
1254 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
1255 pitch, __ttsd_get_engine_error_code(ret));
1256 return TTSD_ERROR_OPERATION_FAILED;
1259 g_cur_engine.default_pitch = pitch;
1264 int ttsd_engine_agent_is_credential_needed(int uid, bool* credential_needed)
1266 if (NULL == credential_needed) {
1267 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid Parameter");
1268 return TTSP_ERROR_INVALID_PARAMETER;
1271 if (false == g_agent_init) {
1272 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1273 return TTSD_ERROR_OPERATION_FAILED;
1276 if (false == g_cur_engine.is_loaded) {
1277 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1278 return TTSD_ERROR_OPERATION_FAILED;
1281 if (NULL == g_cur_engine.pefuncs->need_app_credential) {
1282 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not support to check app credential");
1283 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1286 bool result = false;
1287 result = g_cur_engine.pefuncs->need_app_credential();
1288 *credential_needed = result;
1290 return TTSP_ERROR_NONE;
1293 /******************************************************************************************
1294 * TTS Engine Interfaces for client
1295 *******************************************************************************************/
1297 int ttsd_engine_load_voice(const char* lang, const int vctype)
1299 /* 1. Find voice info */
1301 GSList *iter = NULL;
1302 ttsvoice_s* data = NULL;
1304 iter = g_slist_nth(g_cur_voices, 0);
1305 while (NULL != iter) {
1306 /*Get handle data from list*/
1310 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1311 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1312 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1318 iter = g_slist_next(iter);
1323 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1324 return TTSD_ERROR_OPERATION_FAILED;
1327 /* 2. increse ref count */
1328 data->client_ref_count++;
1330 /* 3. if ref count change 0 to 1 and not default, load voice */
1331 if (1 == data->client_ref_count && false == data->is_default) {
1333 if (NULL != g_cur_engine.pefuncs->load_voice) {
1334 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1336 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load voice : lang(%s), type(%d)",
1337 data->lang, data->type);
1338 data->is_loaded = true;
1340 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1341 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1343 return TTSD_ERROR_OPERATION_FAILED;
1346 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1349 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not load voice : default voice(%d) or ref count(%d)",
1350 data->is_default, data->client_ref_count);
1353 #ifdef ENGINE_AGENT_DEBUG
1354 ttsd_print_voicelist();
1360 int ttsd_engine_unload_voice(const char* lang, const int vctype)
1362 /* 1. Find voice info */
1364 GSList *iter = NULL;
1365 ttsvoice_s* data = NULL;
1367 iter = g_slist_nth(g_cur_voices, 0);
1368 while (NULL != iter) {
1369 /*Get handle data from list*/
1373 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1374 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1375 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1381 iter = g_slist_next(iter);
1386 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1387 return TTSD_ERROR_OPERATION_FAILED;
1390 /* 2. Decrese ref count */
1391 data->client_ref_count--;
1393 /* 3. if ref count change 0 and not default, load voice */
1394 if (0 == data->client_ref_count && false == data->is_default) {
1396 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1397 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1399 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1400 data->lang, data->type);
1401 data->is_loaded = false;
1403 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1404 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1406 return TTSD_ERROR_OPERATION_FAILED;
1409 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1412 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not unload voice : default voice(%d) or ref count(%d)",
1413 data->is_default, data->client_ref_count);
1416 #ifdef ENGINE_AGENT_DEBUG
1417 ttsd_print_voicelist();
1422 int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text, int speed, const char* credential, void* user_param)
1424 if (false == g_agent_init) {
1425 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1426 return TTSD_ERROR_OPERATION_FAILED;
1429 if (false == g_cur_engine.is_loaded) {
1430 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1431 return TTSD_ERROR_OPERATION_FAILED;
1434 /* select voice for default */
1435 char* temp_lang = NULL;
1437 if (true != ttsd_engine_select_valid_voice(lang, vctype, &temp_lang, &temp_type)) {
1438 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to select default voice");
1439 if (NULL != temp_lang) free(temp_lang);
1440 return TTSD_ERROR_INVALID_VOICE;
1442 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Start synthesis : language(%s), type(%d), speed(%d), text(%s), credential(%s)",
1443 temp_lang, temp_type, speed, text, credential);
1446 if (NULL == g_cur_engine.pefuncs->start_synth) {
1447 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] start_synth() of engine is NULL!!");
1448 if (NULL != temp_lang) free(temp_lang);
1449 return TTSD_ERROR_OPERATION_FAILED;
1455 temp_speed = g_cur_engine.default_speed;
1460 /* synthesize text */
1462 ret = g_cur_engine.pefuncs->start_synth(temp_lang, temp_type, text, temp_speed, credential, user_param);
1464 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1465 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] * synthesize error : %s *", __ttsd_get_engine_error_code(ret));
1466 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1467 if (NULL != temp_lang) free(temp_lang);
1471 if (NULL != temp_lang) free(temp_lang);
1475 int ttsd_engine_cancel_synthesis()
1477 if (false == g_agent_init) {
1478 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1479 return TTSD_ERROR_OPERATION_FAILED;
1482 if (false == g_cur_engine.is_loaded) {
1483 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1484 return TTSD_ERROR_OPERATION_FAILED;
1487 if (NULL == g_cur_engine.pefuncs->cancel_synth) {
1488 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] cancel_synth() of engine is NULL!!");
1489 return TTSD_ERROR_OPERATION_FAILED;
1492 /* stop synthesis */
1494 ret = g_cur_engine.pefuncs->cancel_synth();
1496 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail cancel synthesis : %s", __ttsd_get_engine_error_code(ret));
1497 return TTSD_ERROR_OPERATION_FAILED;
1503 bool __supported_voice_cb(const char* language, int type, void* user_data)
1505 GList** voice_list = (GList**)user_data;
1507 if (NULL == language || NULL == voice_list) {
1508 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
1512 voice_s* voice = calloc(1, sizeof(voice_s));
1513 if (NULL == voice) {
1514 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
1517 voice->language = strdup(language);
1520 *voice_list = g_list_append(*voice_list, voice);
1525 int ttsd_engine_get_voice_list(GList** voice_list)
1527 if (false == g_agent_init) {
1528 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1529 return TTSD_ERROR_OPERATION_FAILED;
1532 if (false == g_cur_engine.is_loaded) {
1533 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1534 return TTSD_ERROR_OPERATION_FAILED;
1538 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, (void*)voice_list);
1540 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get voice list : %s", __ttsd_get_engine_error_code(ret));
1541 return TTSD_ERROR_OPERATION_FAILED;
1547 int ttsd_engine_get_default_voice(char** lang, int* vctype)
1549 if (false == g_agent_init) {
1550 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1551 return TTSD_ERROR_OPERATION_FAILED;
1554 if (false == g_cur_engine.is_loaded) {
1555 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1556 return TTSD_ERROR_OPERATION_FAILED;
1559 if (NULL == lang || NULL == vctype) {
1560 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] BAD Parameter");
1561 return TTSD_ERROR_INVALID_PARAMETER;
1564 if (NULL != g_cur_engine.default_lang) {
1565 *lang = strdup(g_cur_engine.default_lang);
1566 *vctype = g_cur_engine.default_vctype;
1568 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice : language(%s), type(%d)", *lang, *vctype);
1570 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Default voice is NULL");
1571 return TTSD_ERROR_OPERATION_FAILED;
1577 int ttsd_engine_set_private_data(const char* key, const char* data)
1579 if (false == g_agent_init) {
1580 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1581 return TTSD_ERROR_OPERATION_FAILED;
1584 if (false == g_cur_engine.is_loaded) {
1585 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No loaded engine");
1586 return TTSD_ERROR_ENGINE_NOT_FOUND;
1590 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid parameter");
1591 return TTSD_ERROR_INVALID_PARAMETER;
1594 if (NULL == g_cur_engine.pefuncs->set_private_data) {
1595 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not supported feature");
1596 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1600 ret = g_cur_engine.pefuncs->set_private_data(key, data);
1602 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set private data(%d)", ret);
1608 int ttsd_engine_get_private_data(const char* key, char** data)
1610 if (false == g_agent_init) {
1611 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1612 return TTSD_ERROR_OPERATION_FAILED;
1615 if (false == g_cur_engine.is_loaded) {
1616 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No loaded engine");
1617 return TTSD_ERROR_ENGINE_NOT_FOUND;
1620 if (NULL == key || NULL == data) {
1621 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Invalid parameter");
1622 return TTSD_ERROR_INVALID_PARAMETER;
1625 if (NULL == g_cur_engine.pefuncs->get_private_data) {
1626 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not supported feature");
1627 return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
1632 ret = g_cur_engine.pefuncs->get_private_data(key, &temp);
1634 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get private data(%d)", ret);
1637 *data = strdup(temp);
1643 void __free_voice_list(GList* voice_list)
1646 voice_s* data = NULL;
1648 /* if list have item */
1649 if (g_list_length(voice_list) > 0) {
1650 /* Get a first item */
1651 iter = g_list_first(voice_list);
1653 while (NULL != iter) {
1657 if (NULL != data->language) free(data->language);
1661 voice_list = g_list_remove_link(voice_list, iter);
1663 iter = g_list_first(voice_list);
1669 * TTS Engine Callback Functions ` *
1671 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)
1673 g_result_cb(event, data, data_size, audio_type, rate, user_data);
1677 /* function for debugging */
1678 int ttsd_print_enginelist()
1681 ttsengine_info_s *data = NULL;
1683 if (g_list_length(g_engine_list) > 0) {
1684 iter = g_list_first(g_engine_list);
1686 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1689 while (NULL != iter) {
1692 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth]", i);
1693 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine uuid : %s", data->engine_uuid);
1694 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine name : %s", data->engine_name);
1695 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine path : %s", data->engine_path);
1696 SECURE_SLOG(LOG_DEBUG, get_tag(), "setting ug path : %s", data->setting_ug_path);
1698 iter = g_list_next(iter);
1701 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1702 SLOG(LOG_DEBUG, get_tag(), " ");
1704 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1705 SLOG(LOG_DEBUG, get_tag(), "No Engine in directory");
1706 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1712 int ttsd_print_voicelist()
1715 GSList *iter = NULL;
1716 ttsvoice_s* data = NULL;
1718 SLOG(LOG_DEBUG, get_tag(), "=== Voice list ===");
1720 if (g_slist_length(g_cur_voices) > 0) {
1721 /* Get a first item */
1722 iter = g_slist_nth(g_cur_voices, 0);
1725 while (NULL != iter) {
1726 /*Get handle data from list*/
1729 if (NULL == data || NULL == data->lang) {
1730 SLOG(LOG_ERROR, get_tag(), "[ERROR] Data is invalid");
1734 SLOG(LOG_DEBUG, get_tag(), "[%dth] default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1735 i, data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1738 iter = g_slist_next(iter);
1743 SLOG(LOG_DEBUG, get_tag(), "==================");