2 * Copyright (c) 2012, 2013 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.
15 #include "ttsd_main.h"
16 #include "ttsd_player.h"
17 #include "ttsd_data.h"
18 #include "ttsd_engine_agent.h"
19 #include "ttsd_server.h"
20 #include "ttsd_dbus_server.h"
22 #include "ttsd_dbus.h"
23 #include "ttsd_config.h"
24 #include "ttsd_network.h"
27 TTSD_SYNTHESIS_CONTROL_DOING = 0,
28 TTSD_SYNTHESIS_CONTROL_DONE = 1,
29 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
30 }ttsd_synthesis_control_e;
37 /* If current engine exist */
38 static bool g_is_engine;
40 /* If engine is running */
41 static ttsd_synthesis_control_e g_synth_control;
43 static Ecore_Timer* g_wait_timer = NULL;
45 /* Function definitions */
46 int __server_next_synthesis(int uid);
49 int __server_set_synth_control(ttsd_synthesis_control_e control)
51 g_synth_control = control;
55 ttsd_synthesis_control_e __server_get_synth_control()
57 return g_synth_control;
60 int __server_send_error(int uid, int utt_id, int error_code)
62 int pid = ttsd_data_get_pid(uid);
65 if ( 0 != ttsdc_send_error_message(pid, uid, utt_id, error_code)) {
66 ttsd_data_delete_client(uid);
72 Eina_Bool __wait_synthesis(void *data)
74 /* get current play */
75 int uid = ttsd_data_is_current_playing();
78 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
82 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
83 /* Start next synthesis */
84 __server_next_synthesis(uid);
94 int __synthesis(int uid)
97 if (0 == ttsd_data_get_speak_data(uid, &sdata)) {
99 utterance_t* utt = (utterance_t*)g_malloc0(sizeof(utterance_t));
102 SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] fail to allocate memory : utterance ", __FUNCTION__);
103 return TTSD_ERROR_OUT_OF_MEMORY;
107 utt->uttid = sdata.utt_id;
109 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
110 SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", utt->uid, utt->uttid );
111 SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", sdata.lang, sdata.vctype, sdata.speed);
112 SLOG(LOG_DEBUG, get_tag(), "Text : %s", sdata.text);
113 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
116 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
117 ret = ttsd_engine_start_synthesis(sdata.lang, sdata.vctype, sdata.text, sdata.speed, (void*)utt);
119 SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] * FAIL to start SYNTHESIS !!!! * ", __FUNCTION__);
121 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
123 ttsd_config_save_error(utt->uid, utt->uttid, sdata.lang, sdata.vctype, sdata.text, __FUNCTION__, __LINE__, "fail to start synthesis");
127 ttsd_server_stop(uid);
129 int pid = ttsd_data_get_pid(uid);
130 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
132 g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
135 if(sdata.text != NULL)
138 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
139 SLOG(LOG_DEBUG, get_tag(), "[Server] Text queue is empty.");
140 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
146 int __server_next_synthesis(int uid)
148 SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY START");
150 /* get current playing client */
151 int current_uid = ttsd_data_get_current_playing();
153 if (0 > current_uid) {
154 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current uid is not valid");
155 SLOG(LOG_DEBUG, get_tag(), "=====");
156 SLOG(LOG_DEBUG, get_tag(), " ");
160 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
161 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running. ");
162 SLOG(LOG_DEBUG, get_tag(), "=====");
163 SLOG(LOG_DEBUG, get_tag(), " ");
167 __synthesis(current_uid);
169 if (0 != ttsd_player_play(current_uid)) {
170 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail ttsd_player_play() ");
172 /* success playing */
173 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
176 SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY END");
177 SLOG(LOG_DEBUG, get_tag(), " ");
183 * TTS Server Callback Functions
186 int __player_result_callback(player_event_e event, int uid, int utt_id)
190 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR][%s] player result error", __FUNCTION__);
191 __server_send_error(uid, utt_id, TTSD_ERROR_OPERATION_FAILED);
192 ttsd_config_save_error(uid, utt_id, NULL, -1, NULL, __FUNCTION__, __LINE__, "PLAYER_ERROR");
194 case PLAYER_EMPTY_SOUND_QUEUE:
195 /* check whether synthesis is running */
196 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
197 /* check text queue is empty */
198 if (0 == ttsd_data_get_speak_data_size(uid) && 0 == ttsd_data_get_sound_data_size(uid)) {
199 SLOG(LOG_DEBUG, get_tag(), "[SERVER Callback] all play completed ");
204 case PLAYER_END_OF_PLAYING:
211 int __synthesis_result_callback(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data)
213 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK START");
215 utterance_t* utt_get_param;
216 utt_get_param = (utterance_t*)user_data;
218 if (NULL == utt_get_param) {
219 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] User data is NULL " );
220 SLOG(LOG_DEBUG, get_tag(), "=====");
221 SLOG(LOG_DEBUG, get_tag(), " ");
225 int uid = utt_get_param->uid;
226 int uttid = utt_get_param->uttid;
228 /* Synthesis is success */
229 if (TTSP_RESULT_EVENT_START == event || TTSP_RESULT_EVENT_CONTINUE == event || TTSP_RESULT_EVENT_FINISH == event) {
231 if (TTSP_RESULT_EVENT_START == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_START");
232 if (TTSP_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CONTINUE");
233 if (TTSP_RESULT_EVENT_FINISH == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_FINISH");
235 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
236 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
237 SLOG(LOG_DEBUG, get_tag(), "=====");
238 SLOG(LOG_DEBUG, get_tag(), " ");
243 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) ",
244 uid, uttid, data, data_size);
247 sound_data_s temp_data;
248 temp_data.data = (char*)g_malloc0( sizeof(char) * data_size );
249 memcpy(temp_data.data, data, data_size);
251 temp_data.data_size = data_size;
252 temp_data.utt_id = utt_get_param->uttid;
253 temp_data.event = event;
255 ttsp_audio_type_e audio_type;
259 if (ttsd_engine_get_audio_format(&audio_type, &rate, &channels)) {
260 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get audio format ");
261 SLOG(LOG_DEBUG, get_tag(), "=====");
262 SLOG(LOG_DEBUG, get_tag(), " ");
266 temp_data.audio_type = audio_type;
267 temp_data.rate = rate;
268 temp_data.channels = channels;
270 if (0 != ttsd_data_add_sound_data(uid, temp_data)) {
271 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", utt_get_param->uid);
274 if (event == TTSP_RESULT_EVENT_FINISH) {
275 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
277 } else if (event == TTSP_RESULT_EVENT_CANCEL) {
278 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CANCEL");
279 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
281 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_ERROR");
282 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
285 if (TTSP_RESULT_EVENT_FINISH == event || TTSP_RESULT_EVENT_CANCEL == event || TTSP_RESULT_EVENT_FAIL == event) {
286 if (NULL != utt_get_param)
290 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK END");
291 SLOG(LOG_DEBUG, get_tag(), " ");
294 if (true == __server_get_is_next_synthesis()) {
295 __server_set_is_next_synthesis(false);
297 /* Do NOT work ecore timer because of This function is thread callbacked */
298 /* need to send dbus message event */
299 ttsd_send_start_next_synthesis();
306 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
308 /* clear client data */
309 ttsd_data_clear_data(uid);
310 ttsd_data_set_client_state(uid, APP_STATE_READY);
313 if ( 0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
315 ttsd_data_delete_client(uid);
321 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
324 case TTS_CONFIG_TYPE_ENGINE:
326 if (NULL == str_param) {
327 SLOG(LOG_ERROR, get_tag(), "[Server] engine id from config is NULL");
331 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
332 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
336 /* stop all player */
337 ttsd_player_all_stop();
339 /* send interrupt message to all clients */
340 ttsd_data_foreach_clients(__get_client_cb, NULL);
342 ttsd_engine_cancel_synthesis();
346 ret = ttsd_engine_setting_set_engine(str_param);
348 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
354 case TTS_CONFIG_TYPE_VOICE:
356 if (NULL == str_param) {
357 SLOG(LOG_ERROR, get_tag(), "[Server] language from config is NULL");
362 ttsp_voice_type_e out_type;
365 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
366 SLOG(LOG_ERROR, get_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
367 ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
369 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
371 if (NULL == out_lang)
374 /* Current language is not available */
375 if (true == ttsd_engine_select_valid_voice("en_US", 2, &out_lang, &out_type)) {
376 ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
378 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
380 if (NULL == out_lang)
387 case TTS_CONFIG_TYPE_SPEED:
389 if (TTSP_SPEED_VERY_SLOW <= int_param && int_param <= TTSP_SPEED_VERY_FAST) {
390 /* set default speed */
392 ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)int_param);
394 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail to set default speed : result(%d)", ret);
412 int ttsd_initialize()
414 if (ttsd_config_initialize(__config_changed_cb)) {
415 SLOG(LOG_ERROR, get_tag(), "[Server WARNING] Fail to initialize config.");
419 if (ttsd_player_init(__player_result_callback)) {
420 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to initialize player init.");
421 return TTSD_ERROR_OPERATION_FAILED;
424 /* Engine Agent initialize */
425 if (0 != ttsd_engine_agent_init(__synthesis_result_callback)) {
426 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to engine agent initialize.");
427 return TTSD_ERROR_OPERATION_FAILED;
430 /* set current engine */
431 if (0 != ttsd_engine_agent_initialize_current_engine()) {
432 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!" );
437 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
439 return TTSD_ERROR_NONE;
444 ttsd_config_finalize();
446 ttsd_player_release();
448 ttsd_engine_agent_release();
450 return TTSD_ERROR_NONE;
453 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
457 result = ttsdc_send_hello(pid, uid);
460 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid);
461 ttsd_server_finalize(uid);
462 } else if (-1 == result) {
463 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error");
470 Eina_Bool ttsd_cleanup_client(void *data)
472 SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
473 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
474 SLOG(LOG_DEBUG, get_tag(), "=====");
475 SLOG(LOG_DEBUG, get_tag(), " ");
481 * TTS Server Functions for Client
484 int ttsd_server_initialize(int pid, int uid)
486 if (false == g_is_engine) {
487 if (0 != ttsd_engine_agent_initialize_current_engine()) {
488 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!! " );
491 return TTSD_ERROR_ENGINE_NOT_FOUND;
497 if (-1 != ttsd_data_is_client(uid)) {
498 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Uid has already been registered ");
499 return TTSD_ERROR_INVALID_PARAMETER;
502 if (0 == ttsd_data_get_client_count()) {
503 if (0 != ttsd_engine_agent_load_current_engine()) {
504 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine ");
505 return TTSD_ERROR_OPERATION_FAILED;
507 /* Check system language */
508 ttsd_config_update_language();
511 if (0 == ttsd_data_get_same_pid_client_count(pid)) {
512 SLOG(LOG_DEBUG, get_tag(), "[Server] open file msg connection");
513 if (0 != ttsd_file_msg_open_connection(pid)) {
514 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail to open file message connection");
515 return TTSD_ERROR_OPERATION_FAILED;
519 if (0 != ttsd_data_new_client(pid, uid)) {
520 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info ");
521 return TTSD_ERROR_OPERATION_FAILED;
524 if (0 != ttsd_player_create_instance(uid)) {
525 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player ");
526 return TTSD_ERROR_OPERATION_FAILED;
529 return TTSD_ERROR_NONE;
532 static Eina_Bool __quit_ecore_loop(void *data)
534 ecore_main_loop_quit();
535 SLOG(LOG_DEBUG, get_tag(), "[Server] quit ecore main loop");
539 int ttsd_server_finalize(int uid)
542 if (0 > ttsd_data_get_client_state(uid, &state)) {
543 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid ");
544 return TTSD_ERROR_INVALID_PARAMETER;
547 ttsd_server_stop(uid);
549 ttsd_player_destroy_instance(uid);
551 int pid = ttsd_data_get_pid(uid);
553 ttsd_data_delete_client(uid);
555 if (0 == ttsd_data_get_same_pid_client_count(pid)) {
556 SLOG(LOG_DEBUG, get_tag(), "[Sever] File msg close connection");
557 ttsd_file_msg_close_connection(pid);
560 /* unload engine, if ref count of client is 0 */
561 if (0 == ttsd_data_get_client_count()) {
562 ecore_timer_add(0, __quit_ecore_loop, NULL);
565 return TTSD_ERROR_NONE;
568 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
571 if (0 > ttsd_data_get_client_state(uid, &state)) {
572 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid ");
573 return TTSD_ERROR_INVALID_PARAMETER;
576 /* check valid voice */
577 char* temp_lang = NULL;
578 ttsp_voice_type_e temp_type;
579 if (true != ttsd_engine_select_valid_voice((const char*)lang, (const ttsp_voice_type_e)voice_type, &temp_lang, &temp_type)) {
580 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice ");
581 return TTSD_ERROR_INVALID_VOICE;
583 if (NULL == temp_lang)
589 data.lang = strdup(lang);
590 data.vctype = (ttsp_voice_type_e)voice_type;
592 data.speed = (ttsp_speed_e)speed;
593 data.utt_id = utt_id;
595 data.text = strdup(text);
597 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
598 if (0 != ttsd_data_add_speak_data(uid, data)) {
599 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : Current state of uid is not 'ready' ");
600 return TTSD_ERROR_OPERATION_FAILED;
603 if (APP_STATE_PLAYING == state) {
604 /* check if engine use network */
605 if (ttsd_engine_agent_need_network()) {
606 if (false == ttsd_network_is_connected()) {
607 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
608 return TTSD_ERROR_OPERATION_FAILED;
612 /* Check whether tts-engine is running or not */
613 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
614 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
620 return TTSD_ERROR_NONE;
623 Eina_Bool __send_interrupt_client(void *data)
625 int* uid = (int*)data;
628 int pid = ttsd_data_get_pid(*uid);
629 /* send message to client about changing state */
630 ttsdc_send_set_state_message (pid, *uid, APP_STATE_PAUSED);
636 int ttsd_server_play(int uid)
639 if (0 > ttsd_data_get_client_state(uid, &state)) {
640 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
641 return TTSD_ERROR_INVALID_PARAMETER;
644 if (APP_STATE_PLAYING == state) {
645 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
646 return TTSD_ERROR_NONE;
649 /* check if engine use network */
650 if (ttsd_engine_agent_need_network()) {
651 if (false == ttsd_network_is_connected()) {
652 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
653 return TTSD_ERROR_OUT_OF_NETWORK;
657 int current_uid = ttsd_data_get_current_playing();
659 if (uid != current_uid && -1 != current_uid) {
660 /* Send interrupt message */
661 SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
664 if (0 != ttsd_player_pause(current_uid)) {
665 SLOG(LOG_WARN, get_tag(), "[Server ERROR] fail to ttsd_player_pause() : uid (%d)", current_uid);
669 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
671 int* temp_uid = (int*)malloc(sizeof(int));
672 *temp_uid = current_uid;
673 ecore_timer_add(0, __send_interrupt_client, temp_uid);
676 /* Change current play */
677 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
678 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
679 return TTSD_ERROR_OPERATION_FAILED;
682 if (APP_STATE_PAUSED == state) {
683 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : Next step is resume player and start synthesis ", uid);
685 ttsd_player_state_e state;
686 if (0 != ttsd_player_get_state(uid, &state)) {
687 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get player state : uid(%d)", uid);
688 return TTSD_ERROR_OPERATION_FAILED;
691 if (TTSD_PLAYER_STATE_PAUSED == state) {
693 if (0 != ttsd_player_resume(uid)) {
694 SLOG(LOG_WARN, get_tag(), "[Server WARNING] fail to ttsd_player_resume()");
696 } else if (TTSD_PLAYER_STATE_NULL == state) {
697 if (0 != ttsd_player_play(uid)) {
698 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail ttsd_player_play() ");
700 /* Need to wait synthesis */
701 if (NULL == g_wait_timer)
702 g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
704 /* success playing */
705 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
712 /* Check whether tts-engine is running or not */
713 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
714 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
719 return TTSD_ERROR_NONE;
723 int ttsd_server_stop(int uid)
726 if (0 > ttsd_data_get_client_state(uid, &state)) {
727 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid ");
728 return TTSD_ERROR_INVALID_PARAMETER;
732 ttsd_data_clear_data(uid);
734 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
735 ttsd_data_set_client_state(uid, APP_STATE_READY);
737 if (0 != ttsd_player_stop(uid))
738 SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
740 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
741 SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running ");
744 ret = ttsd_engine_cancel_synthesis();
746 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
749 if (NULL != g_wait_timer) {
750 SLOG(LOG_DEBUG, get_tag(), "Wait timer is deleted");
751 ecore_timer_del(g_wait_timer);
754 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
756 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is 'ready' ");
759 return TTSD_ERROR_NONE;
762 int ttsd_server_pause(int uid, int* utt_id)
765 if (0 > ttsd_data_get_client_state(uid, &state)) {
766 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid ");
767 return TTSD_ERROR_INVALID_PARAMETER;
770 if (APP_STATE_PLAYING != state) {
771 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play' ");
772 return TTSD_ERROR_INVALID_STATE;
776 ret = ttsd_player_pause(uid);
778 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail player_pause() : ret(%d)", ret);
779 return TTSD_ERROR_OPERATION_FAILED;
782 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
784 return TTSD_ERROR_NONE;
787 int ttsd_server_get_support_voices(int uid, GList** voice_list)
790 if (0 > ttsd_data_get_client_state(uid, &state)) {
791 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid ");
792 return TTSD_ERROR_INVALID_PARAMETER;
796 if (0 != ttsd_engine_get_voice_list(voice_list)) {
797 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() ");
798 return TTSD_ERROR_OPERATION_FAILED;
801 SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices ");
803 return TTSD_ERROR_NONE;
806 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
809 if (0 > ttsd_data_get_client_state(uid, &state)) {
810 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid ");
811 return TTSD_ERROR_INVALID_PARAMETER;
814 /* get current voice */
815 int ret = ttsd_engine_get_default_voice(language, (ttsp_voice_type_e*)voice_type);
817 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail ttsd_server_get_support_voices() ");
821 SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
823 return TTSD_ERROR_NONE;
828 * Server API for Internal event
830 int ttsd_server_start_next_synthesis()
832 /* get current play */
833 int uid = ttsd_data_is_current_playing();
839 return __server_next_synthesis(uid);
844 * TTS Server Functions for Setting *
847 int ttsd_server_setting_initialize(int uid)
849 if (false == g_is_engine) {
850 if (0 != ttsd_engine_agent_initialize_current_engine()) {
851 SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] No Engine !!! " );
853 return TTSD_ERROR_ENGINE_NOT_FOUND;
859 if (-1 != ttsd_setting_data_is_setting(uid)) {
860 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] pid has already been registered ");
861 return TTSD_ERROR_INVALID_PARAMETER;
864 if (0 == ttsd_data_get_client_count()) {
865 if( 0 != ttsd_engine_agent_load_current_engine() ) {
866 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to load current engine ");
867 return TTSD_ERROR_OPERATION_FAILED;
872 if (0 != ttsd_setting_data_add(uid)) {
873 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to add client info ");
874 return TTSD_ERROR_OPERATION_FAILED;
877 return TTSD_ERROR_NONE;
880 int ttsd_server_setting_finalize(int uid)
882 if (-1 == ttsd_setting_data_is_setting(uid)) {
883 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
884 return TTSD_ERROR_INVALID_PARAMETER;
887 ttsd_setting_data_delete(uid);
889 /* unload engine, if ref count of client is 0 */
890 if (0 == ttsd_data_get_client_count()) {
891 ecore_timer_add(0, __quit_ecore_loop, NULL);
894 return TTSD_ERROR_NONE;
897 int ttsd_server_setting_get_engine_list(int uid, GList** engine_list)
899 if (-1 == ttsd_setting_data_is_setting(uid)) {
900 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
901 return TTSD_ERROR_INVALID_PARAMETER;
905 ret = ttsd_engine_setting_get_engine_list(engine_list);
907 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine list : result(%d)", ret);
911 return TTSD_ERROR_NONE;
914 int ttsd_server_setting_get_current_engine(int uid, char** engine_id)
916 if (-1 == ttsd_setting_data_is_setting(uid)) {
917 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
918 return TTSD_ERROR_INVALID_PARAMETER;
922 ret = ttsd_engine_setting_get_engine(engine_id);
924 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get current engine : result(%d) ", ret);
928 return TTSD_ERROR_NONE;
931 int ttsd_server_setting_set_current_engine(int uid, const char* engine_id)
933 /* check if uid is valid */
934 if (-1 == ttsd_setting_data_is_setting(uid)) {
935 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
936 return TTSD_ERROR_INVALID_PARAMETER;
939 if (true == ttsd_engine_agent_is_same_engine(engine_id)) {
940 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
941 return TTSD_ERROR_NONE;
944 /* stop all player */
945 ttsd_player_all_stop();
947 /* send interrupt message to all clients */
948 ttsd_data_foreach_clients(__get_client_cb, NULL);
950 ttsd_engine_cancel_synthesis();
954 ret = ttsd_engine_setting_set_engine(engine_id);
956 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
960 return TTSD_ERROR_NONE;
963 int ttsd_server_setting_get_voice_list(int uid, char** engine_id, GList** voice_list)
965 /* check if uid is valid */
966 if (-1 == ttsd_setting_data_is_setting(uid)) {
967 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
968 return TTSD_ERROR_INVALID_PARAMETER;
971 /* get language list from engine */
973 ret = ttsd_engine_setting_get_voice_list(engine_id, voice_list);
975 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get voice list : result(%d)", ret);
979 return TTSD_ERROR_NONE;
982 int ttsd_server_setting_get_default_voice(int uid, char** language, ttsp_voice_type_e* vctype)
984 /* check if uid is valid */
985 if (-1 == ttsd_setting_data_is_setting(uid)) {
986 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
987 return TTSD_ERROR_INVALID_PARAMETER;
991 ret = ttsd_engine_setting_get_default_voice(language, vctype);
993 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get default voice : result(%d) ", ret);
997 return TTSD_ERROR_NONE;
1000 int ttsd_server_setting_set_default_voice(int uid, const char* language, int vctype)
1002 /* check if uid is valid */
1003 if (-1 == ttsd_setting_data_is_setting(uid)) {
1004 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1005 return TTSD_ERROR_INVALID_PARAMETER;
1008 /* set current language */
1010 ret = ttsd_engine_setting_set_default_voice((const char*)language, (const ttsp_voice_type_e)vctype);
1012 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default voice : result(%d) ", ret);
1016 return TTSD_ERROR_NONE;
1019 int ttsd_server_setting_get_engine_setting(int uid, char** engine_id, GList** engine_setting_list)
1021 /* check if uid is valid */
1022 if (-1 == ttsd_setting_data_is_setting(uid)) {
1023 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1024 return TTSD_ERROR_INVALID_PARAMETER;
1028 ret = ttsd_engine_setting_get_engine_setting_info(engine_id, engine_setting_list);
1030 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine setting info : result(%d)", ret);
1034 return TTSD_ERROR_NONE;
1037 int ttsd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
1039 /* check if uid is valid */
1040 if (-1 == ttsd_setting_data_is_setting(uid)) {
1041 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1042 return TTSD_ERROR_INVALID_PARAMETER;
1046 ret = ttsd_engine_setting_set_engine_setting(key, value);
1048 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set engine setting info : result(%d)", ret);
1052 return TTSD_ERROR_NONE;
1055 int ttsd_server_setting_get_default_speed(int uid, int* default_speed)
1057 /* check if uid is valid */
1058 if (-1 == ttsd_setting_data_is_setting(uid)) {
1059 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1060 return TTSD_ERROR_INVALID_PARAMETER;
1063 /* get current speed */
1065 ret = ttsd_engine_setting_get_default_speed((ttsp_speed_e*)default_speed);
1067 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get default speed : result(%d)", ret);
1071 return TTSD_ERROR_NONE;
1074 int ttsd_server_setting_set_default_speed(int uid, int default_speed)
1076 /* check if uid is valid */
1077 if (-1 == ttsd_setting_data_is_setting(uid)) {
1078 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1079 return TTSD_ERROR_INVALID_PARAMETER;
1082 /* set default speed */
1084 ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)default_speed);
1086 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default speed : result(%d)", ret);
1090 return TTSD_ERROR_NONE;