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.
17 #include "ttsd_config.h"
18 #include "ttsd_data.h"
19 #include "ttsd_dbus.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_engine_agent.h"
22 #include "ttsd_main.h"
23 #include "ttsd_network.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
29 TTSD_SYNTHESIS_CONTROL_DOING = 0,
30 TTSD_SYNTHESIS_CONTROL_DONE = 1,
31 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
32 } ttsd_synthesis_control_e;
39 /* If current engine exist */
40 //static bool g_is_engine;
42 /* If engine is running */
43 static ttsd_synthesis_control_e g_synth_control;
45 static Ecore_Timer* g_wait_timer = NULL;
47 static utterance_t g_utt;
49 static GList *g_proc_list = NULL;
51 /* Function definitions */
52 static int __synthesis(int uid, const char* credential);
54 static int __server_set_synth_control(ttsd_synthesis_control_e control)
56 g_synth_control = control;
60 static ttsd_synthesis_control_e __server_get_synth_control()
62 return g_synth_control;
65 static Eina_Bool __wait_synthesis(void *data)
67 /* get current play */
68 char* credential = (char*)data;
69 int uid = ttsd_data_get_current_playing();
72 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
77 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
78 /* Start next synthesis */
79 __synthesis(uid, credential);
89 static int __synthesis(int uid, const char* credential)
91 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS START");
93 speak_data_s* speak_data = NULL;
94 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
95 if (NULL == speak_data) {
99 int pid = ttsd_data_get_pid(uid);
100 char appid[128] = {0, };
101 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
105 if (NULL == speak_data->lang || NULL == speak_data->text) {
106 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
107 ttsd_server_stop(uid);
109 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
111 if (NULL != speak_data) {
112 if (NULL != speak_data->lang) free(speak_data->lang);
113 if (NULL != speak_data->text) free(speak_data->text);
115 speak_data->lang = NULL;
116 speak_data->text = NULL;
126 g_utt.uttid = speak_data->utt_id;
128 SLOG(LOG_DEBUG, tts_tag(), "-----------------------------------------------------------");
129 SECURE_SLOG(LOG_DEBUG, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
130 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
131 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Text : %s", speak_data->text);
132 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Credential : %s", credential);
133 SLOG(LOG_DEBUG, tts_tag(), "-----------------------------------------------------------");
136 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
137 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
139 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
141 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
143 ttsd_server_stop(uid);
145 int pid = ttsd_data_get_pid(uid);
146 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
148 g_wait_timer = ecore_timer_add(0, __wait_synthesis, (void*)credential);
151 if (NULL != speak_data) {
152 if (NULL != speak_data->lang) free(speak_data->lang);
153 if (NULL != speak_data->text) free(speak_data->text);
155 speak_data->lang = NULL;
156 speak_data->text = NULL;
163 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS END");
164 SLOG(LOG_DEBUG, tts_tag(), " ");
170 * TTS Server Callback Functions
172 int ttsd_send_error(ttse_error_e error, const char* msg)
175 int uttid = g_utt.uttid;
176 int tmp_pid = ttsd_data_get_pid(uid);
178 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, msg);
180 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
182 if (0 != ttsd_player_clear(uid))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
185 if (0 != ttsd_data_clear_data(uid))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
188 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
189 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
191 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
192 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
194 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
195 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
200 int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
203 SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
206 int uttid = g_utt.uttid;
208 /* Synthesis is success */
209 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
210 if (TTSE_RESULT_EVENT_START == event) {
211 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
212 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
213 uid, uttid, data, data_size, audio_type, rate);
214 } else if (TTSE_RESULT_EVENT_FINISH == event) {
215 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
216 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
217 uid, uttid, data, data_size, audio_type, rate);
219 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
223 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
224 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
225 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
226 SLOG(LOG_DEBUG, tts_tag(), "=====");
227 SLOG(LOG_DEBUG, tts_tag(), " ");
228 return TTSD_ERROR_OPERATION_FAILED;
231 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
232 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
233 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
234 SLOG(LOG_DEBUG, tts_tag(), "=====");
235 SLOG(LOG_DEBUG, tts_tag(), " ");
236 return TTSD_ERROR_INVALID_PARAMETER;
240 sound_data_s* temp_sound_data = NULL;
241 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
242 if (NULL == temp_sound_data) {
243 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
244 return TTSD_ERROR_OUT_OF_MEMORY;
247 temp_sound_data->data = NULL;
248 temp_sound_data->rate = 0;
249 temp_sound_data->data_size = 0;
252 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
253 if (NULL != temp_sound_data->data) {
254 memcpy(temp_sound_data->data, data, data_size);
255 temp_sound_data->data_size = data_size;
256 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][free] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
257 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
259 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
262 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
265 temp_sound_data->utt_id = uttid;
266 temp_sound_data->event = event;
267 temp_sound_data->audio_type = audio_type;
268 temp_sound_data->rate = rate;
270 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
271 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
274 if (event == TTSE_RESULT_EVENT_FINISH) {
275 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
278 if (0 != ttsd_player_play(uid)) {
279 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
281 /* Change ready state */
282 ttsd_server_stop(uid);
285 tmp_pid = ttsd_data_get_pid(uid);
286 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
289 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
290 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
294 /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
295 SLOG(LOG_DEBUG, tts_tag(), " ");*/
297 return TTSD_ERROR_NONE;
300 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
302 /* clear client data */
303 ttsd_data_clear_data(uid);
304 ttsd_data_set_client_state(uid, APP_STATE_READY);
307 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
309 ttsd_data_delete_client(uid);
315 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
318 case TTS_CONFIG_TYPE_ENGINE:
320 /* TODO - Determine the policy when engine process get engine changed cb */
321 if (NULL == str_param) {
322 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
327 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
328 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
329 ret = ttsd_engine_agent_unload_current_engine();
331 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
334 ret = ttsd_engine_agent_load_current_engine();
336 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
341 /* stop all player */
342 ttsd_player_all_stop();
344 /* send interrupt message to all clients */
345 ttsd_data_foreach_clients(__get_client_cb, NULL);
347 ttsd_engine_cancel_synthesis();
352 case TTS_CONFIG_TYPE_VOICE:
354 if (NULL == str_param) {
355 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
359 char* out_lang = NULL;
363 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
364 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
365 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
367 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
369 /* Current language is not available */
370 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
372 if (NULL != out_lang) free(out_lang);
376 case TTS_CONFIG_TYPE_SPEED:
378 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
379 /* set default speed */
381 ret = ttsd_engine_agent_set_default_speed(int_param);
383 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
389 case TTS_CONFIG_TYPE_PITCH:
391 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
392 /* set default speed */
394 ret = ttsd_engine_agent_set_default_pitch(int_param);
396 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
406 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
408 SLOG(LOG_DEBUG, tts_tag(), "=== Start to terminate client [%d] ===", uid);
409 ttsd_server_finalize(uid);
413 Eina_Bool ttsd_terminate_daemon(void *data)
415 ttsd_data_foreach_clients(__terminate_client, NULL);
419 void __screen_reader_changed_cb(bool value)
421 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
422 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
423 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
425 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
433 static bool __send_reset_signal(int pid, int uid, app_state_e state, void* user_data)
435 ttsdc_send_error_message(pid, uid, -1, TTSD_ERROR_SERVICE_RESET, "TTS service reset");
439 static void __sig_handler(int signo)
441 /* restore signal handler */
442 signal(signo, SIG_DFL);
444 /* Send error signal via foreach clients */
445 ttsd_data_foreach_clients(__send_reset_signal, NULL);
447 /* invoke signal again */
451 static void __register_sig_handler()
453 signal(SIGSEGV, __sig_handler);
454 signal(SIGABRT, __sig_handler);
455 signal(SIGTERM, __sig_handler);
456 signal(SIGINT, __sig_handler);
457 signal(SIGQUIT, __sig_handler);
460 int ttsd_initialize(ttse_request_callback_s *callback)
462 /* Register signal handler */
463 __register_sig_handler();
465 if (ttsd_config_initialize(__config_changed_cb)) {
466 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
470 if (ttsd_player_init()) {
471 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
472 return TTSD_ERROR_OPERATION_FAILED;
475 /* Engine Agent initialize */
476 if (0 != ttsd_engine_agent_init()) {
477 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
478 return TTSD_ERROR_OPERATION_FAILED;
481 /* set current engine */
482 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
483 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
484 // g_is_engine = false;
486 // g_is_engine = true;
488 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
489 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
490 return TTSD_ERROR_OPERATION_FAILED;
493 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
495 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
496 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
499 return TTSD_ERROR_NONE;
505 if (0 < g_list_length(g_proc_list)) {
506 iter = g_list_first(g_proc_list);
507 while (NULL != iter) {
508 g_proc_list = g_list_remove_link(g_proc_list, iter);
510 iter = g_list_first(g_proc_list);
514 ttsd_config_finalize();
516 ttsd_player_release();
518 ttsd_engine_agent_release();
520 return TTSD_ERROR_NONE;
524 * TTS Server Functions for Client
527 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
529 if (-1 != ttsd_data_is_client(uid)) {
530 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
531 return TTSD_ERROR_NONE;
534 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
535 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
536 return TTSD_ERROR_OPERATION_FAILED;
538 if (0 != ttsd_data_new_client(pid, uid)) {
539 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
540 return TTSD_ERROR_OPERATION_FAILED;
543 if (0 != ttsd_player_create_instance(uid)) {
544 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
545 return TTSD_ERROR_OPERATION_FAILED;
548 return TTSD_ERROR_NONE;
551 static Eina_Bool __quit_ecore_loop(void *data)
553 ttsd_dbus_close_connection();
555 ttsd_network_finalize();
559 ecore_main_loop_quit();
564 static void __read_proc()
568 struct dirent *dirp = NULL;
573 if (0 < g_list_length(g_proc_list)) {
574 iter = g_list_first(g_proc_list);
575 while (NULL != iter) {
576 g_proc_list = g_list_remove_link(g_proc_list, iter);
578 iter = g_list_first(g_proc_list);
582 dp = opendir("/proc");
584 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
587 ret = readdir_r(dp, &entry, &dirp);
589 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to readdir");
594 tmp = atoi(dirp->d_name);
595 if (0 >= tmp) continue;
596 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
598 } while (NULL != dirp);
604 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
610 for (i = 0; i < g_list_length(g_proc_list); i++) {
611 iter = g_list_nth(g_proc_list, i);
613 if (pid == GPOINTER_TO_INT(iter->data)) {
614 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
621 if (false == exist) {
622 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
623 ttsd_server_finalize(uid);
628 char appid[128] = {0, };
629 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
630 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
633 if (0 < strlen(appid)) {
634 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
636 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
639 result = ttsdc_send_hello(pid, uid);
642 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
643 ttsd_server_finalize(uid);
644 } else if (-1 == result) {
645 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
653 Eina_Bool ttsd_cleanup_client(void *data)
655 SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
658 if (0 < ttsd_data_get_client_count()) {
659 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
661 ecore_timer_add(0, __quit_ecore_loop, NULL);
664 SLOG(LOG_DEBUG, tts_tag(), "=====");
665 SLOG(LOG_DEBUG, tts_tag(), " ");
670 void __used_voice_cb(const char* lang, int type)
672 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
673 if (0 != ttsd_engine_unload_voice(lang, type)) {
674 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
678 int ttsd_server_finalize(int uid)
681 if (0 > ttsd_data_get_client_state(uid, &state)) {
682 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
685 ttsd_server_stop(uid);
686 ttsd_player_stop(uid);
688 ttsd_player_destroy_instance(uid);
690 /* Need to unload voice when used voice is unregistered */
691 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
692 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
693 return TTSD_ERROR_OPERATION_FAILED;
696 ttsd_data_delete_client(uid);
698 /* unload engine, if ref count of client is 0 */
699 if (0 == ttsd_data_get_client_count()) {
700 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
701 ecore_timer_add(0, __quit_ecore_loop, NULL);
704 return TTSD_ERROR_NONE;
707 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
710 if (0 > ttsd_data_get_client_state(uid, &state)) {
711 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
712 return TTSD_ERROR_INVALID_PARAMETER;
715 /* check valid voice */
716 char* temp_lang = NULL;
718 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
719 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
720 if (NULL != temp_lang) free(temp_lang);
721 return TTSD_ERROR_INVALID_VOICE;
724 if (NULL == temp_lang) {
725 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
726 return TTSD_ERROR_INVALID_VOICE;
729 speak_data_s* speak_data = NULL;
730 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
731 if (NULL == speak_data) {
732 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
733 if (NULL != temp_lang) free(temp_lang);
734 return TTSD_ERROR_OPERATION_FAILED;
737 speak_data->lang = strdup(lang);
738 speak_data->vctype = voice_type;
740 speak_data->speed = speed;
741 speak_data->utt_id = utt_id;
743 speak_data->text = strdup(text);
745 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
746 if (0 != ttsd_data_add_speak_data(uid, speak_data)) {
747 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
748 if (NULL != temp_lang) free(temp_lang);
749 if (NULL != speak_data) {
750 if (NULL != speak_data->lang) free(speak_data->lang);
751 if (NULL != speak_data->text) free(speak_data->text);
753 speak_data->lang = NULL;
754 speak_data->text = NULL;
760 return TTSD_ERROR_OPERATION_FAILED;
763 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
764 /* Request load voice */
765 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
766 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
767 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
771 if (NULL != temp_lang) free(temp_lang);
773 if (APP_STATE_PLAYING == state) {
774 /* check if engine use network */
775 if (ttsd_engine_agent_need_network()) {
776 if (false == ttsd_network_is_connected()) {
777 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
778 return TTSD_ERROR_OPERATION_FAILED;
782 /* Check whether tts-engine is running or not */
783 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
784 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
786 __synthesis(uid, credential);
790 return TTSD_ERROR_NONE;
793 Eina_Bool __send_interrupt_client(void *data)
795 intptr_t puid = (intptr_t)data;
798 int pid = ttsd_data_get_pid(uid);
800 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
801 /* send message to client about changing state */
802 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
804 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
810 int ttsd_server_play(int uid, const char* credential)
813 if (0 > ttsd_data_get_client_state(uid, &state)) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
815 return TTSD_ERROR_INVALID_PARAMETER;
818 if (APP_STATE_PLAYING == state) {
819 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
820 return TTSD_ERROR_NONE;
823 /* check if engine use network */
824 if (ttsd_engine_agent_need_network()) {
825 if (false == ttsd_network_is_connected()) {
826 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
827 return TTSD_ERROR_OUT_OF_NETWORK;
831 int current_uid = ttsd_data_get_current_playing();
832 SLOG(LOG_ERROR, tts_tag(), "[Server] playing uid (%d)", current_uid);
834 if (uid != current_uid && -1 != current_uid) {
835 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
836 /* Send interrupt message */
837 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
840 if (0 != ttsd_server_stop(current_uid)) {
841 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
843 if (0 != ttsd_player_stop(current_uid)) {
844 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
847 intptr_t pcurrent_uid = (intptr_t)current_uid;
848 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
850 /* Default mode policy of interrupt is "Pause" */
852 /* Send interrupt message */
853 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
856 if (0 != ttsd_player_pause(current_uid)) {
857 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
861 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
863 intptr_t pcurrent_uid = (intptr_t)current_uid;
864 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
868 /* Change current play */
869 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
870 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
871 return TTSD_ERROR_OPERATION_FAILED;
874 if (APP_STATE_PAUSED == state) {
875 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
878 if (0 != ttsd_player_resume(uid)) {
879 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
883 /* Check whether tts-engine is running or not */
884 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
885 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
887 __synthesis(uid, credential);
890 return TTSD_ERROR_NONE;
894 int ttsd_server_stop(int uid)
897 if (0 > ttsd_data_get_client_state(uid, &state)) {
898 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
899 return TTSD_ERROR_INVALID_PARAMETER;
902 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
903 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
904 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
907 ret = ttsd_engine_cancel_synthesis();
909 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
912 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
914 if (0 != ttsd_player_clear(uid))
915 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
917 ttsd_data_set_client_state(uid, APP_STATE_READY);
921 ttsd_data_clear_data(uid);
923 return TTSD_ERROR_NONE;
926 int ttsd_server_pause(int uid, int* utt_id)
929 if (0 > ttsd_data_get_client_state(uid, &state)) {
930 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
931 return TTSD_ERROR_INVALID_PARAMETER;
934 if (APP_STATE_PLAYING != state) {
935 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
936 return TTSD_ERROR_INVALID_STATE;
940 ret = ttsd_player_pause(uid);
942 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
943 return TTSD_ERROR_OPERATION_FAILED;
946 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
948 return TTSD_ERROR_NONE;
951 int ttsd_server_get_support_voices(int uid, GList** voice_list)
954 if (0 > ttsd_data_get_client_state(uid, &state)) {
955 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
956 return TTSD_ERROR_INVALID_PARAMETER;
960 int ret = ttsd_engine_get_voice_list(voice_list);
962 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
966 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
968 return TTSD_ERROR_NONE;
971 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
974 if (0 > ttsd_data_get_client_state(uid, &state)) {
975 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
976 return TTSD_ERROR_INVALID_PARAMETER;
979 /* get current voice */
980 int ret = ttsd_engine_get_default_voice(language, voice_type);
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
986 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
988 return TTSD_ERROR_NONE;
991 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
994 if (0 > ttsd_data_get_client_state(uid, &state)) {
995 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
996 return TTSD_ERROR_INVALID_PARAMETER;
999 if (APP_STATE_READY != state) {
1000 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1001 return TTSD_ERROR_INVALID_STATE;
1004 int ret = ttsd_engine_set_private_data(key, data);
1006 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1008 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data");
1014 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1017 if (0 > ttsd_data_get_client_state(uid, &state)) {
1018 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1019 return TTSD_ERROR_INVALID_PARAMETER;
1022 if (APP_STATE_READY != state) {
1023 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1024 return TTSD_ERROR_INVALID_STATE;
1027 int ret = ttsd_engine_get_private_data(key, data);
1029 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1031 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data");
1037 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1039 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1042 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1044 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1050 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1052 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1055 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1057 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);