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"
28 #define CLIENT_CLEAN_UP_TIME 500
32 TTSD_SYNTHESIS_CONTROL_DOING = 0,
33 TTSD_SYNTHESIS_CONTROL_DONE = 1,
34 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
35 } ttsd_synthesis_control_e;
42 /* If current engine exist */
43 //static bool g_is_engine;
45 /* If engine is running */
46 static ttsd_synthesis_control_e g_synth_control;
48 static Ecore_Timer* g_check_client_timer = NULL;
49 static Ecore_Timer* g_wait_timer = NULL;
51 static utterance_t g_utt;
53 static GList *g_proc_list = NULL;
55 /* Function definitions */
56 static int __synthesis(int uid, const char* credential);
58 static int __server_set_synth_control(ttsd_synthesis_control_e control)
60 g_synth_control = control;
64 static ttsd_synthesis_control_e __server_get_synth_control()
66 return g_synth_control;
69 static Eina_Bool __wait_synthesis(void *data)
71 /* get current play */
72 char* credential = (char*)data;
73 int uid = ttsd_data_get_current_playing();
76 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
80 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
81 /* Start next synthesis */
82 __synthesis(uid, credential);
92 static int __synthesis(int uid, const char* credential)
94 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS START");
96 speak_data_s* speak_data = NULL;
97 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
98 if (NULL == speak_data) {
99 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
103 int pid = ttsd_data_get_pid(uid);
104 char appid[128] = {0, };
105 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
106 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
109 if (NULL == speak_data->lang || NULL == speak_data->text) {
110 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
111 ttsd_server_stop(uid);
113 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
115 if (NULL != speak_data) {
116 if (NULL != speak_data->lang) free(speak_data->lang);
117 if (NULL != speak_data->text) free(speak_data->text);
119 speak_data->lang = NULL;
120 speak_data->text = NULL;
130 g_utt.uttid = speak_data->utt_id;
132 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
133 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
134 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
135 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
136 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
137 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
140 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
141 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
143 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
145 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
147 ttsd_server_stop(uid);
149 int pid = ttsd_data_get_pid(uid);
150 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
152 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
155 if (NULL != speak_data) {
156 if (NULL != speak_data->lang) free(speak_data->lang);
157 if (NULL != speak_data->text) free(speak_data->text);
159 speak_data->lang = NULL;
160 speak_data->text = NULL;
167 SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS END");
168 SLOG(LOG_DEBUG, tts_tag(), " ");
174 * TTS Server Callback Functions
176 int ttsd_send_error(ttse_error_e error, const char* msg)
179 int uttid = g_utt.uttid;
180 int tmp_pid = ttsd_data_get_pid(uid);
182 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, msg);
184 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
186 if (0 != ttsd_player_clear(uid))
187 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
189 if (0 != ttsd_data_clear_data(uid))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
192 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
193 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
195 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
196 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
198 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
199 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
204 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)
206 SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
209 int uttid = g_utt.uttid;
211 /* Synthesis is success */
212 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
213 if (TTSE_RESULT_EVENT_START == event) {
214 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
215 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
216 uid, uttid, data, data_size, audio_type, rate);
217 } else if (TTSE_RESULT_EVENT_FINISH == event) {
218 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
219 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
220 uid, uttid, data, data_size, audio_type, rate);
222 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
226 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
227 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
228 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
229 SLOG(LOG_DEBUG, tts_tag(), "=====");
230 SLOG(LOG_DEBUG, tts_tag(), " ");
231 return TTSD_ERROR_OPERATION_FAILED;
234 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
235 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
236 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
237 SLOG(LOG_DEBUG, tts_tag(), "=====");
238 SLOG(LOG_DEBUG, tts_tag(), " ");
239 return TTSD_ERROR_INVALID_PARAMETER;
243 sound_data_s* temp_sound_data = NULL;
244 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
245 if (NULL == temp_sound_data) {
246 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
247 return TTSD_ERROR_OUT_OF_MEMORY;
250 temp_sound_data->data = NULL;
251 temp_sound_data->rate = 0;
252 temp_sound_data->data_size = 0;
255 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
256 if (NULL != temp_sound_data->data) {
257 memcpy(temp_sound_data->data, data, data_size);
258 temp_sound_data->data_size = data_size;
259 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
260 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
262 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
265 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
268 temp_sound_data->utt_id = uttid;
269 temp_sound_data->event = event;
270 temp_sound_data->audio_type = audio_type;
271 temp_sound_data->rate = rate;
273 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
274 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
277 if (event == TTSE_RESULT_EVENT_FINISH) {
278 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
281 if (0 != ttsd_player_play(uid)) {
282 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
284 /* Change ready state */
285 ttsd_server_stop(uid);
288 tmp_pid = ttsd_data_get_pid(uid);
289 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
292 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
293 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
297 /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
298 SLOG(LOG_DEBUG, tts_tag(), " ");*/
300 return TTSD_ERROR_NONE;
303 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
305 /* clear client data */
306 ttsd_data_clear_data(uid);
307 ttsd_data_set_client_state(uid, APP_STATE_READY);
310 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
312 ttsd_data_delete_client(uid);
318 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
321 case TTS_CONFIG_TYPE_ENGINE:
323 /* TODO - Determine the policy when engine process get engine changed cb */
324 if (NULL == str_param) {
325 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
330 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
331 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
332 ret = ttsd_engine_agent_unload_current_engine();
334 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
337 ret = ttsd_engine_agent_load_current_engine();
339 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
344 /* stop all player */
345 ttsd_player_all_stop();
347 /* send interrupt message to all clients */
348 ttsd_data_foreach_clients(__get_client_cb, NULL);
350 ttsd_engine_cancel_synthesis();
355 case TTS_CONFIG_TYPE_VOICE:
357 if (NULL == str_param) {
358 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
362 char* out_lang = NULL;
366 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
367 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
368 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
370 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
372 /* Current language is not available */
373 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
375 if (NULL != out_lang) {
382 case TTS_CONFIG_TYPE_SPEED:
384 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
385 /* set default speed */
387 ret = ttsd_engine_agent_set_default_speed(int_param);
389 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
395 case TTS_CONFIG_TYPE_PITCH:
397 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
398 /* set default speed */
400 ret = ttsd_engine_agent_set_default_pitch(int_param);
402 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
412 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
414 SLOG(LOG_INFO, tts_tag(), "=== Start to terminate client [%d] ===", uid);
415 ttsd_server_finalize(uid);
419 Eina_Bool ttsd_terminate_daemon(void *data)
421 ttsd_data_foreach_clients(__terminate_client, NULL);
425 void __screen_reader_changed_cb(bool value)
427 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
428 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
429 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
431 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
439 int ttsd_initialize(ttse_request_callback_s *callback)
441 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
443 if (ttsd_config_initialize(__config_changed_cb)) {
444 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
448 if (ttsd_player_init()) {
449 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
450 return TTSD_ERROR_OPERATION_FAILED;
453 /* Engine Agent initialize */
454 if (0 != ttsd_engine_agent_init()) {
455 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
456 return TTSD_ERROR_OPERATION_FAILED;
459 /* set current engine */
460 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
461 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
462 // g_is_engine = false;
464 // g_is_engine = true;
466 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
467 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
468 return TTSD_ERROR_OPERATION_FAILED;
471 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
473 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
474 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
477 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
478 if (NULL == g_check_client_timer) {
479 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
482 return TTSD_ERROR_NONE;
487 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
490 if (0 < g_list_length(g_proc_list)) {
491 iter = g_list_first(g_proc_list);
492 while (NULL != iter) {
493 g_proc_list = g_list_remove_link(g_proc_list, iter);
495 iter = g_list_first(g_proc_list);
499 ttsd_config_finalize();
501 ttsd_player_release();
503 ttsd_engine_agent_release();
505 if (NULL != g_check_client_timer) {
506 ecore_timer_del(g_check_client_timer);
507 g_check_client_timer = NULL;
509 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
512 return TTSD_ERROR_NONE;
516 * TTS Server Functions for Client
519 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
521 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
523 if (-1 != ttsd_data_is_client(uid)) {
524 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
525 return TTSD_ERROR_NONE;
528 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
529 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
530 return TTSD_ERROR_OPERATION_FAILED;
532 if (0 != ttsd_data_new_client(pid, uid)) {
533 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
534 return TTSD_ERROR_OPERATION_FAILED;
537 if (0 != ttsd_player_create_instance(uid)) {
538 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
539 return TTSD_ERROR_OPERATION_FAILED;
542 return TTSD_ERROR_NONE;
545 static Eina_Bool __quit_ecore_loop(void *data)
547 ttsd_dbus_close_connection();
548 ttsd_network_finalize();
550 ecore_main_loop_quit();
556 static void __read_proc()
559 struct dirent *dirp = NULL;
563 if (0 < g_list_length(g_proc_list)) {
564 iter = g_list_first(g_proc_list);
565 while (NULL != iter) {
566 g_proc_list = g_list_remove_link(g_proc_list, iter);
568 iter = g_list_first(g_proc_list);
572 dp = opendir("/proc");
574 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
580 tmp = atoi(dirp->d_name);
581 if (0 >= tmp) continue;
582 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
584 } while (NULL != dirp);
590 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
596 for (i = 0; i < g_list_length(g_proc_list); i++) {
597 iter = g_list_nth(g_proc_list, i);
599 if (pid == GPOINTER_TO_INT(iter->data)) {
600 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
607 if (false == exist) {
608 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
609 ttsd_server_finalize(uid);
614 char appid[128] = {0, };
615 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
616 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
619 if (0 < strlen(appid)) {
620 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
622 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
625 result = ttsdc_send_hello(pid, uid);
628 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
629 ttsd_server_finalize(uid);
630 } else if (-1 == result) {
631 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
639 Eina_Bool ttsd_cleanup_client(void *data)
641 SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
644 if (0 < ttsd_data_get_client_count()) {
645 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
647 ecore_timer_add(0, __quit_ecore_loop, NULL);
650 SLOG(LOG_DEBUG, tts_tag(), "=====");
651 SLOG(LOG_DEBUG, tts_tag(), " ");
656 void __used_voice_cb(const char* lang, int type)
658 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
659 if (0 != ttsd_engine_unload_voice(lang, type)) {
660 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
664 int ttsd_server_finalize(int uid)
666 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
669 if (0 > ttsd_data_get_client_state(uid, &state)) {
670 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
673 ttsd_server_stop(uid);
674 ttsd_player_stop(uid);
676 ttsd_player_destroy_instance(uid);
678 /* Need to unload voice when used voice is unregistered */
679 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
680 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
681 return TTSD_ERROR_OPERATION_FAILED;
684 ttsd_data_delete_client(uid);
686 /* unload engine, if ref count of client is 0 */
687 if (0 == ttsd_data_get_client_count()) {
688 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
689 ecore_timer_add(0, __quit_ecore_loop, NULL);
692 return TTSD_ERROR_NONE;
695 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
698 if (0 > ttsd_data_get_client_state(uid, &state)) {
699 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
700 return TTSD_ERROR_INVALID_PARAMETER;
703 /* check valid voice */
704 char* temp_lang = NULL;
706 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
707 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
708 if (NULL != temp_lang) {
712 return TTSD_ERROR_INVALID_VOICE;
715 if (NULL == temp_lang) {
716 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
717 return TTSD_ERROR_INVALID_VOICE;
720 speak_data_s* speak_data = NULL;
721 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
722 if (NULL == speak_data) {
723 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
724 if (NULL != temp_lang) {
728 return TTSD_ERROR_OPERATION_FAILED;
731 speak_data->lang = strdup(lang);
732 speak_data->vctype = voice_type;
734 speak_data->speed = speed;
735 speak_data->utt_id = utt_id;
737 speak_data->text = strdup(text);
739 SLOG(LOG_INFO, tts_tag(), "[Server] Add queue, lang(%s), vctype(%d), speed(%d), uttid(%d), credential(%s)", lang, voice_type, speed, utt_id, credential);
741 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
743 ret = ttsd_data_add_speak_data(uid, speak_data);
745 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
746 if (NULL != temp_lang) {
750 if (NULL != speak_data) {
751 if (NULL != speak_data->lang) free(speak_data->lang);
752 if (NULL != speak_data->text) free(speak_data->text);
754 speak_data->lang = NULL;
755 speak_data->text = NULL;
764 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
765 /* Request load voice */
766 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
767 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
768 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
772 if (NULL != temp_lang) {
777 if (APP_STATE_PLAYING == state) {
778 /* check if engine use network */
779 if (ttsd_engine_agent_need_network()) {
780 if (false == ttsd_network_is_connected()) {
781 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
782 return TTSD_ERROR_OPERATION_FAILED;
786 /* Check whether tts-engine is running or not */
787 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
788 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
790 __synthesis(uid, credential);
794 return TTSD_ERROR_NONE;
797 Eina_Bool __send_interrupt_client(void *data)
799 intptr_t puid = (intptr_t)data;
802 int pid = ttsd_data_get_pid(uid);
804 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
805 /* send message to client about changing state */
806 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
808 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
814 int ttsd_server_play(int uid, const char* credential)
817 if (0 > ttsd_data_get_client_state(uid, &state)) {
818 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
819 return TTSD_ERROR_INVALID_PARAMETER;
822 if (APP_STATE_PLAYING == state) {
823 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
824 return TTSD_ERROR_NONE;
827 /* check if engine use network */
828 if (ttsd_engine_agent_need_network()) {
829 if (false == ttsd_network_is_connected()) {
830 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
831 return TTSD_ERROR_OUT_OF_NETWORK;
835 int current_uid = ttsd_data_get_current_playing();
836 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
838 if (uid != current_uid && -1 != current_uid) {
839 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
840 /* Send interrupt message */
841 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
844 if (0 != ttsd_server_stop(current_uid)) {
845 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
847 if (0 != ttsd_player_stop(current_uid)) {
848 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
851 intptr_t pcurrent_uid = (intptr_t)current_uid;
852 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
854 /* Default mode policy of interrupt is "Pause" */
856 /* Send interrupt message */
857 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
860 if (0 != ttsd_player_pause(current_uid)) {
861 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
865 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
867 intptr_t pcurrent_uid = (intptr_t)current_uid;
868 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
872 /* Change current play */
873 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
874 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
875 return TTSD_ERROR_OPERATION_FAILED;
878 if (APP_STATE_PAUSED == state) {
879 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
882 if (0 != ttsd_player_resume(uid)) {
883 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
887 /* Check whether tts-engine is running or not */
888 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
889 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
891 __synthesis(uid, credential);
894 return TTSD_ERROR_NONE;
898 int ttsd_server_stop(int uid)
901 if (0 > ttsd_data_get_client_state(uid, &state)) {
902 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
903 return TTSD_ERROR_INVALID_PARAMETER;
906 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
908 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
909 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
910 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
913 ret = ttsd_engine_cancel_synthesis();
915 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
918 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
920 if (0 != ttsd_player_clear(uid))
921 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
923 ttsd_data_set_client_state(uid, APP_STATE_READY);
927 ttsd_data_clear_data(uid);
929 return TTSD_ERROR_NONE;
932 int ttsd_server_pause(int uid, int* utt_id)
935 if (0 > ttsd_data_get_client_state(uid, &state)) {
936 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
937 return TTSD_ERROR_INVALID_PARAMETER;
940 if (APP_STATE_PLAYING != state) {
941 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
942 return TTSD_ERROR_INVALID_STATE;
945 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), utt_id(%d)", uid, utt_id);
948 ret = ttsd_player_pause(uid);
950 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
951 return TTSD_ERROR_OPERATION_FAILED;
954 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
956 return TTSD_ERROR_NONE;
959 int ttsd_server_get_support_voices(int uid, GList** voice_list)
962 if (0 > ttsd_data_get_client_state(uid, &state)) {
963 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
964 return TTSD_ERROR_INVALID_PARAMETER;
968 int ret = ttsd_engine_get_voice_list(voice_list);
970 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
974 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
976 return TTSD_ERROR_NONE;
979 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
982 if (0 > ttsd_data_get_client_state(uid, &state)) {
983 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
984 return TTSD_ERROR_INVALID_PARAMETER;
987 /* get current voice */
988 int ret = ttsd_engine_get_default_voice(language, voice_type);
990 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
994 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
996 return TTSD_ERROR_NONE;
999 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1002 if (0 > ttsd_data_get_client_state(uid, &state)) {
1003 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1004 return TTSD_ERROR_INVALID_PARAMETER;
1007 if (APP_STATE_READY != state) {
1008 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1009 return TTSD_ERROR_INVALID_STATE;
1012 int ret = ttsd_engine_set_private_data(key, data);
1014 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1016 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1022 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1025 if (0 > ttsd_data_get_client_state(uid, &state)) {
1026 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1027 return TTSD_ERROR_INVALID_PARAMETER;
1030 if (APP_STATE_READY != state) {
1031 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1032 return TTSD_ERROR_INVALID_STATE;
1035 int ret = ttsd_engine_get_private_data(key, data);
1037 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1039 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1045 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1047 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1050 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1052 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1058 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1060 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1063 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1065 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);