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)
177 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)
180 SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
183 int uttid = g_utt.uttid;
185 /* Synthesis is success */
186 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
187 if (TTSE_RESULT_EVENT_START == event) {
188 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
189 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
190 uid, uttid, data, data_size, audio_type, rate);
191 } else if (TTSE_RESULT_EVENT_FINISH == event) {
192 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
193 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
194 uid, uttid, data, data_size, audio_type, rate);
196 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
200 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
201 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
202 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
203 SLOG(LOG_DEBUG, tts_tag(), "=====");
204 SLOG(LOG_DEBUG, tts_tag(), " ");
205 return TTSD_ERROR_OPERATION_FAILED;
208 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
209 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
210 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
211 SLOG(LOG_DEBUG, tts_tag(), "=====");
212 SLOG(LOG_DEBUG, tts_tag(), " ");
213 return TTSD_ERROR_INVALID_PARAMETER;
217 sound_data_s* temp_sound_data = NULL;
218 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
219 if (NULL == temp_sound_data) {
220 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
221 return TTSD_ERROR_OUT_OF_MEMORY;
224 temp_sound_data->data = NULL;
225 temp_sound_data->rate = 0;
226 temp_sound_data->data_size = 0;
229 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
230 if (NULL != temp_sound_data->data) {
231 memcpy(temp_sound_data->data, data, data_size);
232 temp_sound_data->data_size = data_size;
233 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][free] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
234 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
236 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
239 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
242 temp_sound_data->utt_id = uttid;
243 temp_sound_data->event = event;
244 temp_sound_data->audio_type = audio_type;
245 temp_sound_data->rate = rate;
247 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
248 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
251 if (event == TTSE_RESULT_EVENT_FINISH) {
252 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
255 if (0 != ttsd_player_play(uid)) {
256 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
258 /* Change ready state */
259 ttsd_server_stop(uid);
262 tmp_pid = ttsd_data_get_pid(uid);
263 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
266 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
267 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
271 /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
272 SLOG(LOG_DEBUG, tts_tag(), " ");*/
274 return TTSD_ERROR_NONE;
277 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
279 /* clear client data */
280 ttsd_data_clear_data(uid);
281 ttsd_data_set_client_state(uid, APP_STATE_READY);
284 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
286 ttsd_data_delete_client(uid);
292 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
295 case TTS_CONFIG_TYPE_ENGINE:
297 /* TODO - Determine the policy when engine process get engine changed cb */
298 if (NULL == str_param) {
299 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
304 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
305 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
306 ret = ttsd_engine_agent_unload_current_engine();
308 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
311 ret = ttsd_engine_agent_load_current_engine();
313 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
318 /* stop all player */
319 ttsd_player_all_stop();
321 /* send interrupt message to all clients */
322 ttsd_data_foreach_clients(__get_client_cb, NULL);
324 ttsd_engine_cancel_synthesis();
329 case TTS_CONFIG_TYPE_VOICE:
331 if (NULL == str_param) {
332 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
336 char* out_lang = NULL;
340 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
341 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
342 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
344 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
346 /* Current language is not available */
347 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
349 if (NULL != out_lang) free(out_lang);
353 case TTS_CONFIG_TYPE_SPEED:
355 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
356 /* set default speed */
358 ret = ttsd_engine_agent_set_default_speed(int_param);
360 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
366 case TTS_CONFIG_TYPE_PITCH:
368 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
369 /* set default speed */
371 ret = ttsd_engine_agent_set_default_pitch(int_param);
373 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
383 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
385 SLOG(LOG_DEBUG, tts_tag(), "=== Start to terminate client [%d] ===", uid);
386 ttsd_server_finalize(uid);
390 Eina_Bool ttsd_terminate_daemon(void *data)
392 ttsd_data_foreach_clients(__terminate_client, NULL);
396 void __screen_reader_changed_cb(bool value)
398 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
399 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
400 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
402 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
410 static bool __send_reset_signal(int pid, int uid, app_state_e state, void* user_data)
412 ttsdc_send_error_message(pid, uid, -1, TTSD_ERROR_SERVICE_RESET, "TTS service reset");
416 static void __sig_handler(int signo)
418 /* restore signal handler */
419 signal(signo, SIG_DFL);
421 /* Send error signal via foreach clients */
422 ttsd_data_foreach_clients(__send_reset_signal, NULL);
424 /* invoke signal again */
428 static void __register_sig_handler()
430 signal(SIGSEGV, __sig_handler);
431 signal(SIGABRT, __sig_handler);
432 signal(SIGTERM, __sig_handler);
433 signal(SIGINT, __sig_handler);
434 signal(SIGQUIT, __sig_handler);
437 int ttsd_initialize(ttse_request_callback_s *callback)
439 /* Register signal handler */
440 __register_sig_handler();
442 if (ttsd_config_initialize(__config_changed_cb)) {
443 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
447 if (ttsd_player_init()) {
448 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
449 return TTSD_ERROR_OPERATION_FAILED;
452 /* Engine Agent initialize */
453 if (0 != ttsd_engine_agent_init()) {
454 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
455 return TTSD_ERROR_OPERATION_FAILED;
458 /* set current engine */
459 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
460 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
461 // g_is_engine = false;
463 // g_is_engine = true;
465 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
466 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
467 return TTSD_ERROR_OPERATION_FAILED;
470 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
472 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
473 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
476 return TTSD_ERROR_NONE;
482 if (0 < g_list_length(g_proc_list)) {
483 iter = g_list_first(g_proc_list);
484 while (NULL != iter) {
485 g_proc_list = g_list_remove_link(g_proc_list, iter);
487 iter = g_list_first(g_proc_list);
491 ttsd_config_finalize();
493 ttsd_player_release();
495 ttsd_engine_agent_release();
497 return TTSD_ERROR_NONE;
501 * TTS Server Functions for Client
504 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
506 if (-1 != ttsd_data_is_client(uid)) {
507 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
508 return TTSD_ERROR_NONE;
511 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
512 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
513 return TTSD_ERROR_OPERATION_FAILED;
515 if (0 != ttsd_data_new_client(pid, uid)) {
516 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
517 return TTSD_ERROR_OPERATION_FAILED;
520 if (0 != ttsd_player_create_instance(uid)) {
521 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
522 return TTSD_ERROR_OPERATION_FAILED;
525 return TTSD_ERROR_NONE;
528 static Eina_Bool __quit_ecore_loop(void *data)
530 ttsd_dbus_close_connection();
532 ttsd_network_finalize();
536 ecore_main_loop_quit();
541 static void __read_proc()
545 struct dirent *dirp = NULL;
550 if (0 < g_list_length(g_proc_list)) {
551 iter = g_list_first(g_proc_list);
552 while (NULL != iter) {
553 g_proc_list = g_list_remove_link(g_proc_list, iter);
555 iter = g_list_first(g_proc_list);
559 dp = opendir("/proc");
561 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
564 ret = readdir_r(dp, &entry, &dirp);
566 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to readdir");
571 tmp = atoi(dirp->d_name);
572 if (0 >= tmp) continue;
573 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
575 } while (NULL != dirp);
581 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
587 for (i = 0; i < g_list_length(g_proc_list); i++) {
588 iter = g_list_nth(g_proc_list, i);
590 if (pid == GPOINTER_TO_INT(iter->data)) {
591 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
598 if (false == exist) {
599 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
600 ttsd_server_finalize(uid);
605 char appid[128] = {0, };
606 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
607 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
610 if (0 < strlen(appid)) {
611 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
613 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
616 result = ttsdc_send_hello(pid, uid);
619 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
620 ttsd_server_finalize(uid);
621 } else if (-1 == result) {
622 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
630 Eina_Bool ttsd_cleanup_client(void *data)
632 SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
635 if (0 < ttsd_data_get_client_count()) {
636 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
638 ecore_timer_add(0, __quit_ecore_loop, NULL);
641 SLOG(LOG_DEBUG, tts_tag(), "=====");
642 SLOG(LOG_DEBUG, tts_tag(), " ");
647 void __used_voice_cb(const char* lang, int type)
649 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
650 if (0 != ttsd_engine_unload_voice(lang, type)) {
651 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
655 int ttsd_server_finalize(int uid)
658 if (0 > ttsd_data_get_client_state(uid, &state)) {
659 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
662 ttsd_server_stop(uid);
663 ttsd_player_stop(uid);
665 ttsd_player_destroy_instance(uid);
667 /* Need to unload voice when used voice is unregistered */
668 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
669 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
670 return TTSD_ERROR_OPERATION_FAILED;
673 ttsd_data_delete_client(uid);
675 /* unload engine, if ref count of client is 0 */
676 if (0 == ttsd_data_get_client_count()) {
677 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
678 ecore_timer_add(0, __quit_ecore_loop, NULL);
681 return TTSD_ERROR_NONE;
684 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
687 if (0 > ttsd_data_get_client_state(uid, &state)) {
688 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
689 return TTSD_ERROR_INVALID_PARAMETER;
692 /* check valid voice */
693 char* temp_lang = NULL;
695 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
696 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
697 if (NULL != temp_lang) free(temp_lang);
698 return TTSD_ERROR_INVALID_VOICE;
701 if (NULL == temp_lang) {
702 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
703 return TTSD_ERROR_INVALID_VOICE;
706 speak_data_s* speak_data = NULL;
707 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
708 if (NULL == speak_data) {
709 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
710 if (NULL != temp_lang) free(temp_lang);
711 return TTSD_ERROR_OPERATION_FAILED;
714 speak_data->lang = strdup(lang);
715 speak_data->vctype = voice_type;
717 speak_data->speed = speed;
718 speak_data->utt_id = utt_id;
720 speak_data->text = strdup(text);
722 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
723 if (0 != ttsd_data_add_speak_data(uid, speak_data)) {
724 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
725 if (NULL != temp_lang) free(temp_lang);
726 if (NULL != speak_data) {
727 if (NULL != speak_data->lang) free(speak_data->lang);
728 if (NULL != speak_data->text) free(speak_data->text);
730 speak_data->lang = NULL;
731 speak_data->text = NULL;
737 return TTSD_ERROR_OPERATION_FAILED;
740 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
741 /* Request load voice */
742 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
743 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
744 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
748 if (NULL != temp_lang) free(temp_lang);
750 if (APP_STATE_PLAYING == state) {
751 /* check if engine use network */
752 if (ttsd_engine_agent_need_network()) {
753 if (false == ttsd_network_is_connected()) {
754 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
755 return TTSD_ERROR_OPERATION_FAILED;
759 /* Check whether tts-engine is running or not */
760 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
761 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
763 __synthesis(uid, credential);
767 return TTSD_ERROR_NONE;
770 Eina_Bool __send_interrupt_client(void *data)
772 intptr_t puid = (intptr_t)data;
775 int pid = ttsd_data_get_pid(uid);
777 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
778 /* send message to client about changing state */
779 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
781 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
787 int ttsd_server_play(int uid, const char* credential)
790 if (0 > ttsd_data_get_client_state(uid, &state)) {
791 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
792 return TTSD_ERROR_INVALID_PARAMETER;
795 if (APP_STATE_PLAYING == state) {
796 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
797 return TTSD_ERROR_NONE;
800 /* check if engine use network */
801 if (ttsd_engine_agent_need_network()) {
802 if (false == ttsd_network_is_connected()) {
803 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
804 return TTSD_ERROR_OUT_OF_NETWORK;
808 int current_uid = ttsd_data_get_current_playing();
809 SLOG(LOG_ERROR, tts_tag(), "[Server] playing uid (%d)", current_uid);
811 if (uid != current_uid && -1 != current_uid) {
812 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
813 /* Send interrupt message */
814 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
817 if (0 != ttsd_server_stop(current_uid)) {
818 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
820 if (0 != ttsd_player_stop(current_uid)) {
821 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
824 intptr_t pcurrent_uid = (intptr_t)current_uid;
825 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
827 /* Default mode policy of interrupt is "Pause" */
829 /* Send interrupt message */
830 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
833 if (0 != ttsd_player_pause(current_uid)) {
834 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
838 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
840 intptr_t pcurrent_uid = (intptr_t)current_uid;
841 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
845 /* Change current play */
846 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
847 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
848 return TTSD_ERROR_OPERATION_FAILED;
851 if (APP_STATE_PAUSED == state) {
852 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
855 if (0 != ttsd_player_resume(uid)) {
856 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
860 /* Check whether tts-engine is running or not */
861 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
862 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
864 __synthesis(uid, credential);
867 return TTSD_ERROR_NONE;
871 int ttsd_server_stop(int uid)
874 if (0 > ttsd_data_get_client_state(uid, &state)) {
875 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
876 return TTSD_ERROR_INVALID_PARAMETER;
879 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
880 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
881 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
884 ret = ttsd_engine_cancel_synthesis();
886 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
889 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
891 if (0 != ttsd_player_clear(uid))
892 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
894 ttsd_data_set_client_state(uid, APP_STATE_READY);
898 ttsd_data_clear_data(uid);
900 return TTSD_ERROR_NONE;
903 int ttsd_server_pause(int uid, int* utt_id)
906 if (0 > ttsd_data_get_client_state(uid, &state)) {
907 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
908 return TTSD_ERROR_INVALID_PARAMETER;
911 if (APP_STATE_PLAYING != state) {
912 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
913 return TTSD_ERROR_INVALID_STATE;
917 ret = ttsd_player_pause(uid);
919 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
920 return TTSD_ERROR_OPERATION_FAILED;
923 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
925 return TTSD_ERROR_NONE;
928 int ttsd_server_get_support_voices(int uid, GList** voice_list)
931 if (0 > ttsd_data_get_client_state(uid, &state)) {
932 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
933 return TTSD_ERROR_INVALID_PARAMETER;
937 int ret = ttsd_engine_get_voice_list(voice_list);
939 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
943 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
945 return TTSD_ERROR_NONE;
948 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
951 if (0 > ttsd_data_get_client_state(uid, &state)) {
952 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
953 return TTSD_ERROR_INVALID_PARAMETER;
956 /* get current voice */
957 int ret = ttsd_engine_get_default_voice(language, voice_type);
959 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
963 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
965 return TTSD_ERROR_NONE;
968 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
971 if (0 > ttsd_data_get_client_state(uid, &state)) {
972 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
973 return TTSD_ERROR_INVALID_PARAMETER;
976 if (APP_STATE_READY != state) {
977 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
978 return TTSD_ERROR_INVALID_STATE;
981 int ret = ttsd_engine_set_private_data(key, data);
983 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
985 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data");
991 int ttsd_server_get_private_data(int uid, const char* key, 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_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1001 return TTSD_ERROR_INVALID_STATE;
1004 int ret = ttsd_engine_get_private_data(key, data);
1006 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1008 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data");
1014 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1016 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1019 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1021 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1027 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1029 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1032 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1034 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);