2 * Copyright (c) 2011-2014 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"
30 TTSD_SYNTHESIS_CONTROL_DOING = 0,
31 TTSD_SYNTHESIS_CONTROL_DONE = 1,
32 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
33 } ttsd_synthesis_control_e;
40 /* If current engine exist */
41 static bool g_is_engine;
43 /* If engine is running */
44 static ttsd_synthesis_control_e g_synth_control;
46 static Ecore_Timer* g_wait_timer = NULL;
48 static utterance_t g_utt;
50 static GList *g_proc_list = NULL;
52 /* Function definitions */
53 static int __synthesis(int uid);
55 static int __server_set_synth_control(ttsd_synthesis_control_e control)
57 g_synth_control = control;
61 static ttsd_synthesis_control_e __server_get_synth_control()
63 return g_synth_control;
66 static Eina_Bool __wait_synthesis(void *data)
68 /* get current play */
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 */
89 static int __synthesis(int uid)
91 SLOG(LOG_DEBUG, get_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 if (NULL == speak_data->lang || NULL == speak_data->text) {
100 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Current data is NOT valid");
101 ttsd_server_stop(uid);
103 int pid = ttsd_data_get_pid(uid);
104 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
106 if (NULL != speak_data) {
107 if (NULL != speak_data->lang) free(speak_data->lang);
108 if (NULL != speak_data->text) free(speak_data->text);
110 speak_data->lang = NULL;
111 speak_data->text = NULL;
121 g_utt.uttid = speak_data->utt_id;
123 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
124 SECURE_SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
125 SECURE_SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
126 SECURE_SLOG(LOG_DEBUG, get_tag(), "Text : %s", speak_data->text);
127 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
130 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
131 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, NULL);
133 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
135 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
137 ttsd_server_stop(uid);
139 int pid = ttsd_data_get_pid(uid);
140 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
142 g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
145 if (NULL != speak_data) {
146 if (NULL != speak_data->lang) free(speak_data->lang);
147 if (NULL != speak_data->text) free(speak_data->text);
149 speak_data->lang = NULL;
150 speak_data->text = NULL;
157 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS END");
158 SLOG(LOG_DEBUG, get_tag(), " ");
164 * TTS Server Callback Functions
166 int __synthesis_result_callback(ttsp_result_event_e event, const void* data, unsigned int data_size,
167 ttsp_audio_type_e audio_type, int rate, void *user_data)
169 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK START");
172 int uttid = g_utt.uttid;
174 /* Synthesis is success */
175 if (TTSP_RESULT_EVENT_START == event || TTSP_RESULT_EVENT_CONTINUE == event || TTSP_RESULT_EVENT_FINISH == event) {
177 if (TTSP_RESULT_EVENT_START == event) {
178 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_START");
179 SECURE_SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
180 uid, uttid, data, data_size, audio_type, rate);
181 } else if (TTSP_RESULT_EVENT_FINISH == event) {
182 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_FINISH");
183 SECURE_SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
184 uid, uttid, data, data_size, audio_type, rate);
186 /*if (TTSP_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CONTINUE");*/
190 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
191 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
192 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
193 SLOG(LOG_DEBUG, get_tag(), "=====");
194 SLOG(LOG_DEBUG, get_tag(), " ");
198 if (rate <= 0 || audio_type < 0 || audio_type > TTSP_AUDIO_TYPE_MAX) {
199 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
200 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] audio data is invalid");
201 SLOG(LOG_DEBUG, get_tag(), "=====");
202 SLOG(LOG_DEBUG, get_tag(), " ");
207 sound_data_s* temp_sound_data = NULL;
208 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
209 if (NULL == temp_sound_data) {
210 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Out of memory");
214 temp_sound_data->data = NULL;
215 temp_sound_data->rate = 0;
216 temp_sound_data->data_size = 0;
219 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
220 if (NULL != temp_sound_data->data) {
221 memcpy(temp_sound_data->data, data, data_size);
222 temp_sound_data->data_size = data_size;
223 SLOG(LOG_ERROR, get_tag(), "[DEBUG][free] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
224 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
226 SLOG(LOG_ERROR, get_tag(), "Fail to allocate memory");
229 SLOG(LOG_ERROR, get_tag(), "Sound data is NULL");
232 temp_sound_data->utt_id = uttid;
233 temp_sound_data->event = event;
234 temp_sound_data->audio_type = audio_type;
235 temp_sound_data->rate = rate;
237 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
238 SECURE_SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
241 if (event == TTSP_RESULT_EVENT_FINISH) {
242 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
245 if (0 != ttsd_player_play(uid)) {
246 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
248 /* Change ready state */
249 ttsd_server_stop(uid);
252 tmp_pid = ttsd_data_get_pid(uid);
253 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
256 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_ERROR");
257 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
261 /*SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK END");
262 SLOG(LOG_DEBUG, get_tag(), " ");*/
267 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
269 /* clear client data */
270 ttsd_data_clear_data(uid);
271 ttsd_data_set_client_state(uid, APP_STATE_READY);
274 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
276 ttsd_data_delete_client(uid);
282 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
285 case TTS_CONFIG_TYPE_ENGINE:
287 if (NULL == str_param) {
288 SLOG(LOG_ERROR, get_tag(), "[Server] engine id from config is NULL");
293 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
294 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine");
295 ret = ttsd_engine_agent_unload_current_engine();
297 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
300 ret = ttsd_engine_agent_load_current_engine();
302 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
307 /* stop all player */
308 ttsd_player_all_stop();
310 /* send interrupt message to all clients */
311 ttsd_data_foreach_clients(__get_client_cb, NULL);
313 ttsd_engine_cancel_synthesis();
316 ret = ttsd_engine_agent_set_default_engine(str_param);
318 SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] Fail to set current engine : result(%d)", ret);
324 case TTS_CONFIG_TYPE_VOICE:
326 if (NULL == str_param) {
327 SLOG(LOG_ERROR, get_tag(), "[Server] language from config is NULL");
331 char* out_lang = NULL;
335 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
336 SLOG(LOG_ERROR, get_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
337 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
339 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
341 /* Current language is not available */
342 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
344 if (NULL != out_lang) free(out_lang);
348 case TTS_CONFIG_TYPE_SPEED:
350 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
351 /* set default speed */
353 ret = ttsd_engine_agent_set_default_speed(int_param);
355 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
361 case TTS_CONFIG_TYPE_PITCH:
363 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
364 /* set default speed */
366 ret = ttsd_engine_agent_set_default_pitch(int_param);
368 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
381 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
383 SLOG(LOG_DEBUG, get_tag(), "=== Start to terminate client [%d] ===", uid);
384 ttsd_server_finalize(uid);
388 Eina_Bool ttsd_terminate_daemon(void *data)
390 ttsd_data_foreach_clients(__terminate_client, NULL);
394 void __screen_reader_changed_cb(bool value)
396 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
397 SLOG(LOG_DEBUG, get_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
398 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
400 SLOG(LOG_DEBUG, get_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
409 int ttsd_initialize()
411 if (ttsd_config_initialize(__config_changed_cb)) {
412 SLOG(LOG_ERROR, get_tag(), "[Server WARNING] Fail to initialize config.");
416 if (ttsd_player_init()) {
417 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to initialize player init.");
418 return TTSD_ERROR_OPERATION_FAILED;
421 /* Engine Agent initialize */
422 if (0 != ttsd_engine_agent_init(__synthesis_result_callback)) {
423 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to engine agent initialize.");
424 return TTSD_ERROR_OPERATION_FAILED;
427 /* set current engine */
428 if (0 != ttsd_engine_agent_initialize_current_engine()) {
429 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!");
434 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
436 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
437 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
440 return TTSD_ERROR_NONE;
446 if (0 < g_list_length(g_proc_list)) {
447 iter = g_list_first(g_proc_list);
448 while (NULL != iter) {
449 g_proc_list = g_list_remove_link(g_proc_list, iter);
451 iter = g_list_first(g_proc_list);
455 ttsd_config_finalize();
457 ttsd_player_release();
459 ttsd_engine_agent_release();
461 return TTSD_ERROR_NONE;
464 static void __read_proc()
468 struct dirent *dirp = NULL;
473 if (0 < g_list_length(g_proc_list)) {
474 iter = g_list_first(g_proc_list);
475 while (NULL != iter) {
476 g_proc_list = g_list_remove_link(g_proc_list, iter);
478 iter = g_list_first(g_proc_list);
482 dp = opendir("/proc");
484 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to open proc");
487 ret = readdir_r(dp, &entry, &dirp);
489 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to readdir");
494 tmp = atoi(dirp->d_name);
495 if (0 >= tmp) continue;
496 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
498 } while (NULL != dirp);
504 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
510 for (i = 0; i < g_list_length(g_proc_list); i++) {
511 iter = g_list_nth(g_proc_list, i);
513 if (pid == GPOINTER_TO_INT(iter->data)) {
514 SLOG(LOG_DEBUG, get_tag(), "uid (%d) is running", uid);
521 if (false == exist) {
522 SLOG(LOG_ERROR, get_tag(), "uid (%d) should be removed", uid);
523 ttsd_server_finalize(uid);
528 char appid[128] = {0, };
529 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
530 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get app id");
533 if (0 < strlen(appid)) {
534 SLOG(LOG_DEBUG, get_tag(), "[%d] is running app - %s", pid, appid);
536 SLOG(LOG_DEBUG, get_tag(), "[%d] is daemon or no_running app", pid);
539 result = ttsdc_send_hello(pid, uid);
542 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid);
543 ttsd_server_finalize(uid);
544 } else if (-1 == result) {
545 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error");
553 Eina_Bool ttsd_cleanup_client(void *data)
555 SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
557 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
558 SLOG(LOG_DEBUG, get_tag(), "=====");
559 SLOG(LOG_DEBUG, get_tag(), " ");
565 * TTS Server Functions for Client
568 int ttsd_server_initialize(int pid, int uid)
570 if (false == g_is_engine) {
571 if (0 != ttsd_engine_agent_initialize_current_engine()) {
572 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!");
575 return TTSD_ERROR_ENGINE_NOT_FOUND;
581 if (-1 != ttsd_data_is_client(uid)) {
582 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Uid has already been registered");
583 return TTSD_ERROR_NONE;
586 if (0 == ttsd_data_get_client_count()) {
587 if (0 != ttsd_engine_agent_load_current_engine()) {
588 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine");
589 return TTSD_ERROR_OPERATION_FAILED;
593 if (0 != ttsd_data_new_client(pid, uid)) {
594 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info");
595 return TTSD_ERROR_OPERATION_FAILED;
598 if (0 != ttsd_player_create_instance(uid)) {
599 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player");
600 return TTSD_ERROR_OPERATION_FAILED;
603 return TTSD_ERROR_NONE;
606 static Eina_Bool __quit_ecore_loop(void *data)
608 ecore_main_loop_quit();
612 void __used_voice_cb(const char* lang, int type)
614 SLOG(LOG_DEBUG, get_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
615 if (0 != ttsd_engine_unload_voice(lang, type)) {
616 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to unload voice");
620 int ttsd_server_finalize(int uid)
623 if (0 > ttsd_data_get_client_state(uid, &state)) {
624 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
627 ttsd_server_stop(uid);
628 ttsd_player_stop(uid);
630 ttsd_player_destroy_instance(uid);
632 /* Need to unload voice when used voice is unregistered */
633 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
634 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set used voice");
635 return TTSD_ERROR_OPERATION_FAILED;
638 ttsd_data_delete_client(uid);
640 /* unload engine, if ref count of client is 0 */
641 if (0 == ttsd_data_get_client_count()) {
642 SLOG(LOG_DEBUG, get_tag(), "[Server] Quit main loop");
643 ecore_timer_add(0, __quit_ecore_loop, NULL);
646 return TTSD_ERROR_NONE;
649 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
652 if (0 > ttsd_data_get_client_state(uid, &state)) {
653 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
654 return TTSD_ERROR_INVALID_PARAMETER;
657 /* check valid voice */
658 char* temp_lang = NULL;
660 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
661 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice");
662 if (NULL != temp_lang) free(temp_lang);
663 return TTSD_ERROR_INVALID_VOICE;
666 if (NULL == temp_lang) {
667 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
668 return TTSD_ERROR_INVALID_VOICE;
671 speak_data_s* speak_data = NULL;
672 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
673 if (NULL == speak_data) {
674 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to allocate memory");
675 if (NULL != temp_lang) free(temp_lang);
676 return TTSD_ERROR_OPERATION_FAILED;
679 speak_data->lang = strdup(lang);
680 speak_data->vctype = voice_type;
682 speak_data->speed = speed;
683 speak_data->utt_id = utt_id;
685 speak_data->text = strdup(text);
687 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
688 if (0 != ttsd_data_add_speak_data(uid, speak_data)) {
689 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add speak data");
690 if (NULL != temp_lang) free(temp_lang);
691 if (NULL != speak_data) {
692 if (NULL != speak_data->lang) free(speak_data->lang);
693 if (NULL != speak_data->text) free(speak_data->text);
695 speak_data->lang = NULL;
696 speak_data->text = NULL;
702 return TTSD_ERROR_OPERATION_FAILED;
705 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
706 /* Request load voice */
707 SLOG(LOG_DEBUG, get_tag(), "[Server] Request to load voice");
708 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
709 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load voice");
713 if (NULL != temp_lang) free(temp_lang);
715 if (APP_STATE_PLAYING == state) {
716 /* check if engine use network */
717 if (ttsd_engine_agent_need_network()) {
718 if (false == ttsd_network_is_connected()) {
719 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
720 return TTSD_ERROR_OPERATION_FAILED;
724 /* Check whether tts-engine is running or not */
725 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
726 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
732 return TTSD_ERROR_NONE;
735 Eina_Bool __send_interrupt_client(void *data)
737 intptr_t puid = (intptr_t)data;
740 int pid = ttsd_data_get_pid(uid);
742 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
743 /* send message to client about changing state */
744 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
746 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
752 int ttsd_server_play(int uid)
755 if (0 > ttsd_data_get_client_state(uid, &state)) {
756 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
757 return TTSD_ERROR_INVALID_PARAMETER;
760 if (APP_STATE_PLAYING == state) {
761 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
762 return TTSD_ERROR_NONE;
765 /* check if engine use network */
766 if (ttsd_engine_agent_need_network()) {
767 if (false == ttsd_network_is_connected()) {
768 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
769 return TTSD_ERROR_OUT_OF_NETWORK;
773 int current_uid = ttsd_data_get_current_playing();
774 SLOG(LOG_ERROR, get_tag(), "[Server] playing uid (%d)", current_uid);
776 if (uid != current_uid && -1 != current_uid) {
777 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
778 /* Send interrupt message */
779 SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
782 if (0 != ttsd_server_stop(current_uid)) {
783 SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
785 if (0 != ttsd_player_stop(current_uid)) {
786 SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
789 intptr_t pcurrent_uid = (intptr_t)current_uid;
790 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
792 /* Default mode policy of interrupt is "Pause" */
794 /* Send interrupt message */
795 SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
798 if (0 != ttsd_player_pause(current_uid)) {
799 SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
803 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
805 intptr_t pcurrent_uid = (intptr_t)current_uid;
806 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
810 /* Change current play */
811 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
812 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
813 return TTSD_ERROR_OPERATION_FAILED;
816 if (APP_STATE_PAUSED == state) {
817 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
820 if (0 != ttsd_player_resume(uid)) {
821 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
825 /* Check whether tts-engine is running or not */
826 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
827 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
832 return TTSD_ERROR_NONE;
836 int ttsd_server_stop(int uid)
839 if (0 > ttsd_data_get_client_state(uid, &state)) {
840 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
841 return TTSD_ERROR_INVALID_PARAMETER;
844 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
845 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
846 SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running");
849 ret = ttsd_engine_cancel_synthesis();
851 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
854 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
856 if (0 != ttsd_player_clear(uid))
857 SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
859 ttsd_data_set_client_state(uid, APP_STATE_READY);
863 ttsd_data_clear_data(uid);
865 return TTSD_ERROR_NONE;
868 int ttsd_server_pause(int uid, int* utt_id)
871 if (0 > ttsd_data_get_client_state(uid, &state)) {
872 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
873 return TTSD_ERROR_INVALID_PARAMETER;
876 if (APP_STATE_PLAYING != state) {
877 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play'");
878 return TTSD_ERROR_INVALID_STATE;
882 ret = ttsd_player_pause(uid);
884 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
885 return TTSD_ERROR_OPERATION_FAILED;
888 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
890 return TTSD_ERROR_NONE;
893 int ttsd_server_get_support_voices(int uid, GList** voice_list)
896 if (0 > ttsd_data_get_client_state(uid, &state)) {
897 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
898 return TTSD_ERROR_INVALID_PARAMETER;
902 if (0 != ttsd_engine_get_voice_list(voice_list)) {
903 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
904 return TTSD_ERROR_OPERATION_FAILED;
907 SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices");
909 return TTSD_ERROR_NONE;
912 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
915 if (0 > ttsd_data_get_client_state(uid, &state)) {
916 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
917 return TTSD_ERROR_INVALID_PARAMETER;
920 /* get current voice */
921 int ret = ttsd_engine_get_default_voice(language, voice_type);
923 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
927 SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
929 return TTSD_ERROR_NONE;