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"
32 /* If current engine exist */
33 static bool g_is_engine;
35 /* If engine is running */
36 static bool g_is_synthesizing;
38 /* If the daemon get the result */
39 static bool g_is_next_synthesis = false;
41 /* Function definitions */
42 int __server_next_synthesis(int uid);
45 int __server_set_is_synthesizing(bool flag)
47 g_is_synthesizing = flag;
51 bool __server_get_is_synthesizing()
53 return g_is_synthesizing;
56 int __server_set_is_next_synthesis(bool flag)
58 g_is_next_synthesis = flag;
62 bool __server_get_is_next_synthesis()
64 return g_is_next_synthesis;
67 int __server_send_error(int uid, int utt_id, int error_code)
69 int pid = ttsd_data_get_pid(uid);
72 if ( 0 != ttsdc_send_error_message(pid, uid, utt_id, error_code)) {
73 ttsd_data_delete_client(uid);
79 int __server_start_synthesis(int uid, int mode)
83 /* check if tts-engine is running */
84 if (true == __server_get_is_synthesizing()) {
85 SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running ");
88 if (0 == ttsd_data_get_speak_data(uid, &sdata)) {
89 utterance_t* utt = (utterance_t*)g_malloc0(sizeof(utterance_t));
92 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Out of memory : utterance ");
93 return TTSD_ERROR_OUT_OF_MEMORY;
97 utt->uttid = sdata.utt_id;
99 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
100 SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", utt->uid, utt->uttid );
101 SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", sdata.lang, sdata.vctype, sdata.speed);
102 SLOG(LOG_DEBUG, get_tag(), "Text : %s", sdata.text);
103 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
105 __server_set_is_synthesizing(true);
107 ret = ttsd_engine_start_synthesis(sdata.lang, sdata.vctype, sdata.text, sdata.speed, (void*)utt);
109 SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] * FAIL to start SYNTHESIS !!!! * ", __FUNCTION__);
111 __server_set_is_synthesizing(false);
113 result = TTSD_ERROR_OPERATION_FAILED;
117 /* mode 2 : Add text in playing state */
119 __server_send_error(uid, sdata.utt_id, TTSD_ERROR_OPERATION_FAILED);
120 ttsd_config_save_error(utt->uid, utt->uttid, sdata.lang, sdata.vctype, sdata.text, __FUNCTION__, __LINE__, "fail to start synthesis (mode=2)");
122 ttsd_server_stop(uid);
124 int pid = ttsd_data_get_pid(uid);
125 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
128 SLOG(LOG_DEBUG, get_tag(), "[Server] SUCCESS to start synthesis");
131 if(sdata.text != NULL)
135 SLOG(LOG_DEBUG, get_tag(), "[Server] Text List is EMPTY!! ");
142 int __server_play_internal(int uid, app_state_e state)
146 /* - input uid is current play */
150 if (APP_STATE_PAUSED == state) {
151 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : Next step is resume player and start synthesis ", uid);
153 /* resume player and start speech synthesis */
154 if (0 != ttsd_player_resume(uid)) {
155 SLOG(LOG_WARN, get_tag(), "[Server WARNING] fail to ttsd_player_resume()");
158 /* mode 1 for play */
159 ret = __server_start_synthesis(uid, 1);
161 } else if(APP_STATE_READY == state) {
162 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Ready' state : Next step is start synthesis ", uid);
164 /* mode 1 for play */
165 ret = __server_start_synthesis(uid, 1);
173 int __server_next_synthesis(int uid)
175 SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY START");
177 /* get current playing client */
178 int current_uid = ttsd_data_get_current_playing();
180 if (0 > current_uid) {
181 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current uid is not valid");
182 SLOG(LOG_DEBUG, get_tag(), "=====");
183 SLOG(LOG_DEBUG, get_tag(), " ");
187 if (true == __server_get_is_synthesizing()) {
188 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running. ");
189 SLOG(LOG_DEBUG, get_tag(), "=====");
190 SLOG(LOG_DEBUG, get_tag(), " ");
194 /* synthesize next text */
196 if (0 == ttsd_data_get_speak_data(current_uid, &sdata)) {
198 utterance_t* utt = (utterance_t*)g_malloc0(sizeof(utterance_t));
201 SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] fail to allocate memory : utterance ", __FUNCTION__);
202 return TTSD_ERROR_OUT_OF_MEMORY;
205 utt->uid = current_uid;
206 utt->uttid = sdata.utt_id;
208 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
209 SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", utt->uid, utt->uttid );
210 SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", sdata.lang, sdata.vctype, sdata.speed);
211 SLOG(LOG_DEBUG, get_tag(), "Text : %s", sdata.text);
212 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
214 __server_set_is_synthesizing(true);
217 ret = ttsd_engine_start_synthesis(sdata.lang, sdata.vctype, sdata.text, sdata.speed, (void*)utt);
219 SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] * FAIL to start SYNTHESIS !!!! * ", __FUNCTION__);
221 __server_set_is_synthesizing(false);
223 __server_send_error(current_uid, sdata.utt_id, TTSD_ERROR_OPERATION_FAILED);
224 ttsd_config_save_error(utt->uid, utt->uttid, sdata.lang, sdata.vctype, sdata.text, __FUNCTION__, __LINE__, "fail to start synthesis");
228 ttsd_server_stop(current_uid);
230 int pid = ttsd_data_get_pid(current_uid);
231 ttsdc_send_set_state_message(pid, current_uid, APP_STATE_READY);
234 if(sdata.text != NULL)
237 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
238 SLOG(LOG_DEBUG, get_tag(), "[Server] Text queue is empty.");
239 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
242 if (0 != ttsd_player_play(current_uid)) {
243 SLOG(LOG_WARN, get_tag(), "[Server WARNING] __server_next_synthesis : fail ttsd_player_play() ");
245 /* success playing */
246 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
249 SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY END");
250 SLOG(LOG_DEBUG, get_tag(), " ");
256 * TTS Server Callback Functions
259 int __player_result_callback(player_event_e event, int uid, int utt_id)
263 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR][%s] player result error", __FUNCTION__);
264 __server_send_error(uid, utt_id, TTSD_ERROR_OPERATION_FAILED);
265 ttsd_config_save_error(uid, utt_id, NULL, -1, NULL, __FUNCTION__, __LINE__, "PLAYER_ERROR");
268 case PLAYER_EMPTY_SOUND_QUEUE:
269 /* check whether synthesis is running */
270 if (false == __server_get_is_synthesizing()) {
271 /* check text queue is empty */
272 if (0 == ttsd_data_get_speak_data_size(uid) && 0 == ttsd_data_get_sound_data_size(uid)) {
273 SLOG(LOG_DEBUG, get_tag(), "[SERVER Callback] all play completed ");
278 case PLAYER_END_OF_PLAYING:
285 int __synthesis_result_callback(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data)
287 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK START");
289 utterance_t* utt_get_param;
290 utt_get_param = (utterance_t*)user_data;
292 if (NULL == utt_get_param) {
293 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] User data is NULL " );
294 SLOG(LOG_DEBUG, get_tag(), "=====");
295 SLOG(LOG_DEBUG, get_tag(), " ");
299 int uid = utt_get_param->uid;
300 int uttid = utt_get_param->uttid;
302 /* Synthesis is success */
303 if (TTSP_RESULT_EVENT_START == event || TTSP_RESULT_EVENT_CONTINUE == event || TTSP_RESULT_EVENT_FINISH == event) {
305 if (TTSP_RESULT_EVENT_START == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_START");
306 if (TTSP_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CONTINUE");
307 if (TTSP_RESULT_EVENT_FINISH == event) SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_FINISH");
309 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
310 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
311 SLOG(LOG_DEBUG, get_tag(), "=====");
312 SLOG(LOG_DEBUG, get_tag(), " ");
317 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) ",
318 uid, uttid, data, data_size);
321 sound_data_s temp_data;
322 temp_data.data = (char*)g_malloc0( sizeof(char) * data_size );
323 memcpy(temp_data.data, data, data_size);
325 temp_data.data_size = data_size;
326 temp_data.utt_id = utt_get_param->uttid;
327 temp_data.event = event;
329 ttsp_audio_type_e audio_type;
333 if (ttsd_engine_get_audio_format(&audio_type, &rate, &channels)) {
334 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get audio format ");
335 SLOG(LOG_DEBUG, get_tag(), "=====");
336 SLOG(LOG_DEBUG, get_tag(), " ");
340 temp_data.audio_type = audio_type;
341 temp_data.rate = rate;
342 temp_data.channels = channels;
344 if (0 != ttsd_data_add_sound_data(uid, temp_data)) {
345 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", utt_get_param->uid);
348 if (event == TTSP_RESULT_EVENT_FINISH) {
349 __server_set_is_synthesizing(false);
350 __server_set_is_next_synthesis(true);
352 } else if (event == TTSP_RESULT_EVENT_CANCEL) {
353 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CANCEL");
354 __server_set_is_synthesizing(false);
355 __server_set_is_next_synthesis(true);
357 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event ERROR");
358 __server_set_is_synthesizing(false);
359 __server_set_is_next_synthesis(true);
362 if (TTSP_RESULT_EVENT_FINISH == event || TTSP_RESULT_EVENT_CANCEL == event || TTSP_RESULT_EVENT_FAIL == event) {
363 if (NULL != utt_get_param)
367 SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK END");
368 SLOG(LOG_DEBUG, get_tag(), " ");
370 if (true == __server_get_is_next_synthesis()) {
371 __server_set_is_next_synthesis(false);
373 /* Do NOT work ecore timer because of This function is thread callbacked */
374 /* need to send dbus message event */
375 ttsd_send_start_next_synthesis();
381 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
383 /* clear client data */
384 ttsd_data_clear_data(uid);
385 ttsd_data_set_client_state(uid, APP_STATE_READY);
388 if ( 0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
390 ttsd_data_delete_client(uid);
396 void __config_lang_changed_cb(const char* language, int type)
399 ttsp_voice_type_e out_type;
402 if (true == ttsd_engine_select_valid_voice(language, type, &out_lang, &out_type)) {
403 SLOG(LOG_ERROR, get_tag(), "[Server] vaild language : lang(%s), type(%d)", out_lang, out_type);
404 ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
406 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set vaild language : lang(%s), type(%d)", out_lang, out_type);
408 if (NULL == out_lang)
411 /* Current language is not available */
412 if (true == ttsd_engine_select_valid_voice("en_US", type, &out_lang, &out_type)) {
413 ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
415 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set vaild language : lang(%s), type(%d)", out_lang, out_type);
417 if (NULL == out_lang)
430 int ttsd_initialize()
432 if (ttsd_config_initialize(__config_lang_changed_cb)) {
433 SLOG(LOG_ERROR, get_tag(), "[Server WARNING] Fail to initialize config.");
437 if (ttsd_player_init(__player_result_callback)) {
438 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to initialize player init.");
439 return TTSD_ERROR_OPERATION_FAILED;
442 /* Engine Agent initialize */
443 if (0 != ttsd_engine_agent_init(__synthesis_result_callback)) {
444 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to engine agent initialize.");
445 return TTSD_ERROR_OPERATION_FAILED;
448 /* set current engine */
449 if (0 != ttsd_engine_agent_initialize_current_engine()) {
450 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!" );
455 return TTSD_ERROR_NONE;
460 ttsd_config_finalize();
462 ttsd_player_release();
464 ttsd_engine_agent_release();
466 return TTSD_ERROR_NONE;
469 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
473 result = ttsdc_send_hello(pid, uid);
476 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid);
477 ttsd_server_finalize(uid);
478 } else if (-1 == result) {
479 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error");
486 Eina_Bool ttsd_cleanup_client(void *data)
488 SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
489 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
490 SLOG(LOG_DEBUG, get_tag(), "=====");
491 SLOG(LOG_DEBUG, get_tag(), " ");
497 * TTS Server Functions for Client
500 int ttsd_server_initialize(int pid, int uid)
502 if (false == g_is_engine) {
503 if (0 != ttsd_engine_agent_initialize_current_engine()) {
504 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!! " );
507 return TTSD_ERROR_ENGINE_NOT_FOUND;
513 if (-1 != ttsd_data_is_client(uid)) {
514 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Uid has already been registered ");
515 return TTSD_ERROR_INVALID_PARAMETER;
518 if (0 == ttsd_data_get_client_count()) {
519 if (0 != ttsd_engine_agent_load_current_engine()) {
520 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine ");
521 return TTSD_ERROR_OPERATION_FAILED;
524 /* Check system language */
525 ttsd_config_update_language();
528 if (0 != ttsd_data_new_client(pid, uid)) {
529 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info ");
530 return TTSD_ERROR_OPERATION_FAILED;
533 if (0 != ttsd_player_create_instance(uid)) {
534 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player ");
535 return TTSD_ERROR_OPERATION_FAILED;
538 return TTSD_ERROR_NONE;
541 static Eina_Bool __quit_ecore_loop(void *data)
543 ecore_main_loop_quit();
544 SLOG(LOG_DEBUG, get_tag(), "[Server] quit ecore main loop");
548 int ttsd_server_finalize(int uid)
551 if (0 > ttsd_data_get_client_state(uid, &state)) {
552 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid ");
553 return TTSD_ERROR_INVALID_PARAMETER;
556 ttsd_server_stop(uid);
558 ttsd_player_destroy_instance(uid);
560 ttsd_data_delete_client(uid);
562 /* unload engine, if ref count of client is 0 */
563 if (0 == ttsd_data_get_client_count()) {
564 ecore_timer_add(0, __quit_ecore_loop, NULL);
567 return TTSD_ERROR_NONE;
570 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
573 if (0 > ttsd_data_get_client_state(uid, &state)) {
574 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid ");
575 return TTSD_ERROR_INVALID_PARAMETER;
578 /* check valid voice */
579 char* temp_lang = NULL;
580 ttsp_voice_type_e temp_type;
581 if (true != ttsd_engine_select_valid_voice((const char*)lang, (const ttsp_voice_type_e)voice_type, &temp_lang, &temp_type)) {
582 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice ");
583 return TTSD_ERROR_INVALID_VOICE;
585 if (NULL == temp_lang)
591 data.lang = strdup(lang);
592 data.vctype = (ttsp_voice_type_e)voice_type;
594 data.speed = (ttsp_speed_e)speed;
595 data.utt_id = utt_id;
597 data.text = strdup(text);
599 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
600 if (0 != ttsd_data_add_speak_data(uid, data)) {
601 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : Current state of uid is not 'ready' ");
602 return TTSD_ERROR_OPERATION_FAILED;
605 if (APP_STATE_PLAYING == state) {
606 /* check if engine use network */
607 if (ttsd_engine_agent_need_network()) {
608 if (false == ttsd_network_is_connected()) {
609 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
610 return TTSD_ERROR_OPERATION_FAILED;
614 /* mode 2 for add text */
615 if (0 != __server_start_synthesis(uid, 2)) {
616 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail to schedule synthesis : uid(%d)", uid);
617 return TTSD_ERROR_OPERATION_FAILED;
621 return TTSD_ERROR_NONE;
624 Eina_Bool __send_interrupt_client(void *data)
626 int* uid = (int*)data;
629 int pid = ttsd_data_get_pid(*uid);
630 /* send message to client about changing state */
631 ttsdc_send_set_state_message (pid, *uid, APP_STATE_PAUSED);
637 int ttsd_server_play(int uid)
640 if (0 > ttsd_data_get_client_state(uid, &state)) {
641 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
642 return TTSD_ERROR_INVALID_PARAMETER;
645 if (APP_STATE_PLAYING == state) {
646 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
647 return TTSD_ERROR_NONE;
650 /* check if engine use network */
651 if (ttsd_engine_agent_need_network()) {
652 if (false == ttsd_network_is_connected()) {
653 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
654 return TTSD_ERROR_OUT_OF_NETWORK;
658 int current_uid = ttsd_data_get_current_playing();
660 if (uid != current_uid && -1 != current_uid) {
661 /* Send interrupt message */
662 SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
665 if (0 != ttsd_player_pause(current_uid)) {
666 SLOG(LOG_WARN, get_tag(), "[Server ERROR] fail to ttsd_player_pause() : uid (%d)", current_uid);
670 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
672 int* temp_uid = (int*)malloc(sizeof(int));
673 *temp_uid = current_uid;
674 ecore_timer_add(0, __send_interrupt_client, temp_uid);
677 /* Change current play */
678 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
679 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
680 return TTSD_ERROR_OPERATION_FAILED;
683 if (0 != __server_play_internal(uid, state)) {
684 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to start synthesis : uid(%d)", uid);
685 return TTSD_ERROR_OPERATION_FAILED;
688 return TTSD_ERROR_NONE;
692 int ttsd_server_stop(int uid)
695 if (0 > ttsd_data_get_client_state(uid, &state)) {
696 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid ");
697 return TTSD_ERROR_INVALID_PARAMETER;
701 ttsd_data_clear_data(uid);
703 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
704 ttsd_data_set_client_state(uid, APP_STATE_READY);
706 if (0 != ttsd_player_stop(uid))
707 SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
709 if (true == __server_get_is_synthesizing()) {
710 SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running ");
713 ret = ttsd_engine_cancel_synthesis();
715 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
717 __server_set_is_synthesizing(false);
720 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is 'ready' ");
723 return TTSD_ERROR_NONE;
726 int ttsd_server_pause(int uid, int* utt_id)
729 if (0 > ttsd_data_get_client_state(uid, &state)) {
730 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid ");
731 return TTSD_ERROR_INVALID_PARAMETER;
734 if (APP_STATE_PLAYING != state) {
735 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play' ");
736 return TTSD_ERROR_INVALID_STATE;
740 ret = ttsd_player_pause(uid);
742 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail player_pause() : ret(%d)", ret);
743 return TTSD_ERROR_OPERATION_FAILED;
746 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
748 return TTSD_ERROR_NONE;
751 int ttsd_server_get_support_voices(int uid, GList** voice_list)
754 if (0 > ttsd_data_get_client_state(uid, &state)) {
755 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid ");
756 return TTSD_ERROR_INVALID_PARAMETER;
760 if (0 != ttsd_engine_get_voice_list(voice_list)) {
761 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() ");
762 return TTSD_ERROR_OPERATION_FAILED;
765 SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices ");
767 return TTSD_ERROR_NONE;
770 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
773 if (0 > ttsd_data_get_client_state(uid, &state)) {
774 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid ");
775 return TTSD_ERROR_INVALID_PARAMETER;
778 /* get current voice */
779 int ret = ttsd_engine_get_default_voice(language, (ttsp_voice_type_e*)voice_type);
781 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail ttsd_server_get_support_voices() ");
785 SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
787 return TTSD_ERROR_NONE;
791 * Server API for Internal event
794 int ttsd_server_start_next_synthesis()
796 /* get current play */
797 int uid = ttsd_data_is_current_playing();
803 return __server_next_synthesis(uid);
807 * TTS Server Functions for Setting *
810 int ttsd_server_setting_initialize(int uid)
812 if (false == g_is_engine) {
813 if (0 != ttsd_engine_agent_initialize_current_engine()) {
814 SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] No Engine !!! " );
816 return TTSD_ERROR_ENGINE_NOT_FOUND;
822 if (-1 != ttsd_setting_data_is_setting(uid)) {
823 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] pid has already been registered ");
824 return TTSD_ERROR_INVALID_PARAMETER;
827 if (0 == ttsd_data_get_client_count()) {
828 if( 0 != ttsd_engine_agent_load_current_engine() ) {
829 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to load current engine ");
830 return TTSD_ERROR_OPERATION_FAILED;
835 if (0 != ttsd_setting_data_add(uid)) {
836 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to add client info ");
837 return TTSD_ERROR_OPERATION_FAILED;
840 return TTSD_ERROR_NONE;
843 int ttsd_server_setting_finalize(int uid)
845 if (-1 == ttsd_setting_data_is_setting(uid)) {
846 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
847 return TTSD_ERROR_INVALID_PARAMETER;
850 ttsd_setting_data_delete(uid);
852 /* unload engine, if ref count of client is 0 */
853 if (0 == ttsd_data_get_client_count()) {
854 ecore_timer_add(0, __quit_ecore_loop, NULL);
857 return TTSD_ERROR_NONE;
860 int ttsd_server_setting_get_engine_list(int uid, GList** engine_list)
862 if (-1 == ttsd_setting_data_is_setting(uid)) {
863 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
864 return TTSD_ERROR_INVALID_PARAMETER;
868 ret = ttsd_engine_setting_get_engine_list(engine_list);
870 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine list : result(%d)", ret);
874 return TTSD_ERROR_NONE;
877 int ttsd_server_setting_get_current_engine(int uid, char** engine_id)
879 if (-1 == ttsd_setting_data_is_setting(uid)) {
880 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
881 return TTSD_ERROR_INVALID_PARAMETER;
885 ret = ttsd_engine_setting_get_engine(engine_id);
887 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get current engine : result(%d) ", ret);
891 return TTSD_ERROR_NONE;
894 int ttsd_server_setting_set_current_engine(int uid, const char* engine_id)
896 /* check if uid is valid */
897 if (-1 == ttsd_setting_data_is_setting(uid)) {
898 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
899 return TTSD_ERROR_INVALID_PARAMETER;
902 if (true == ttsd_engine_agent_is_same_engine(engine_id)) {
903 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
904 return TTSD_ERROR_NONE;
907 /* stop all player */
908 ttsd_player_all_stop();
910 /* send interrupt message to all clients */
911 ttsd_data_foreach_clients(__get_client_cb, NULL);
913 ttsd_engine_cancel_synthesis();
917 ret = ttsd_engine_setting_set_engine(engine_id);
919 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
923 return TTSD_ERROR_NONE;
926 int ttsd_server_setting_get_voice_list(int uid, char** engine_id, GList** voice_list)
928 /* check if uid is valid */
929 if (-1 == ttsd_setting_data_is_setting(uid)) {
930 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
931 return TTSD_ERROR_INVALID_PARAMETER;
934 /* get language list from engine */
936 ret = ttsd_engine_setting_get_voice_list(engine_id, voice_list);
938 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get voice list : result(%d)", ret);
942 return TTSD_ERROR_NONE;
945 int ttsd_server_setting_get_default_voice(int uid, char** language, ttsp_voice_type_e* vctype)
947 /* check if uid is valid */
948 if (-1 == ttsd_setting_data_is_setting(uid)) {
949 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
950 return TTSD_ERROR_INVALID_PARAMETER;
954 ret = ttsd_engine_setting_get_default_voice(language, vctype);
956 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get default voice : result(%d) ", ret);
960 return TTSD_ERROR_NONE;
963 int ttsd_server_setting_set_default_voice(int uid, const char* language, int vctype)
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 /* set current language */
973 ret = ttsd_engine_setting_set_default_voice((const char*)language, (const ttsp_voice_type_e)vctype);
975 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default voice : result(%d) ", ret);
979 return TTSD_ERROR_NONE;
982 int ttsd_server_setting_get_engine_setting(int uid, char** engine_id, GList** engine_setting_list)
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_engine_setting_info(engine_id, engine_setting_list);
993 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine setting info : result(%d)", ret);
997 return TTSD_ERROR_NONE;
1000 int ttsd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
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;
1009 ret = ttsd_engine_setting_set_engine_setting(key, value);
1011 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set engine setting info : result(%d)", ret);
1015 return TTSD_ERROR_NONE;
1018 int ttsd_server_setting_get_default_speed(int uid, int* default_speed)
1020 /* check if uid is valid */
1021 if (-1 == ttsd_setting_data_is_setting(uid)) {
1022 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid (%s)", uid);
1023 return TTSD_ERROR_INVALID_PARAMETER;
1026 /* get current speed */
1028 ret = ttsd_engine_setting_get_default_speed((ttsp_speed_e*)default_speed);
1030 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get default speed : result(%d)", ret);
1034 return TTSD_ERROR_NONE;
1037 int ttsd_server_setting_set_default_speed(int uid, int default_speed)
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;
1045 /* set default speed */
1047 ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)default_speed);
1049 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default speed : result(%d)", ret);
1053 return TTSD_ERROR_NONE;