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");
173 * TTS Server Callback Functions
175 int ttsd_send_error(ttse_error_e error, const char* msg)
178 int uttid = g_utt.uttid;
179 int tmp_pid = ttsd_data_get_pid(uid);
181 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, (NULL == msg ? "NULL" : msg));
183 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
185 if (0 != ttsd_player_clear(uid))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
188 if (0 != ttsd_data_clear_data(uid))
189 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
191 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
192 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
194 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
195 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
197 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
198 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
203 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)
205 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
208 int uttid = g_utt.uttid;
210 /* Synthesis is success */
211 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
212 if (TTSE_RESULT_EVENT_START == event) {
213 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
214 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
215 uid, uttid, data, data_size, audio_type, rate);
216 } else if (TTSE_RESULT_EVENT_FINISH == event) {
217 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
218 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
219 uid, uttid, data, data_size, audio_type, rate);
221 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
225 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
226 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
227 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
228 SLOG(LOG_DEBUG, tts_tag(), "@@@");
229 return TTSD_ERROR_OPERATION_FAILED;
232 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
233 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
234 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
235 SLOG(LOG_DEBUG, tts_tag(), "@@@");
236 return TTSD_ERROR_INVALID_PARAMETER;
240 sound_data_s* temp_sound_data = NULL;
241 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
242 if (NULL == temp_sound_data) {
243 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
244 return TTSD_ERROR_OUT_OF_MEMORY;
247 temp_sound_data->data = NULL;
248 temp_sound_data->rate = 0;
249 temp_sound_data->data_size = 0;
252 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
253 if (NULL != temp_sound_data->data) {
254 memcpy(temp_sound_data->data, data, data_size);
255 temp_sound_data->data_size = data_size;
256 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
257 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
259 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
262 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
265 temp_sound_data->utt_id = uttid;
266 temp_sound_data->event = event;
267 temp_sound_data->audio_type = audio_type;
268 temp_sound_data->rate = rate;
270 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
271 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
274 if (event == TTSE_RESULT_EVENT_FINISH) {
275 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
278 if (0 != ttsd_player_play(uid)) {
279 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
281 /* Change ready state */
282 ttsd_server_stop(uid);
285 tmp_pid = ttsd_data_get_pid(uid);
286 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
289 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
290 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
294 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
295 SLOG(LOG_DEBUG, tts_tag(), " ");*/
297 return TTSD_ERROR_NONE;
300 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
302 /* clear client data */
303 ttsd_data_clear_data(uid);
304 ttsd_data_set_client_state(uid, APP_STATE_READY);
307 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
309 ttsd_data_delete_client(uid);
315 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
318 case TTS_CONFIG_TYPE_ENGINE:
320 /* TODO - Determine the policy when engine process get engine changed cb */
321 if (NULL == str_param) {
322 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
327 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
328 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
329 ret = ttsd_engine_agent_unload_current_engine();
331 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
334 ret = ttsd_engine_agent_load_current_engine();
336 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
341 /* stop all player */
342 ttsd_player_all_stop();
344 /* send interrupt message to all clients */
345 ttsd_data_foreach_clients(__get_client_cb, NULL);
347 ttsd_engine_cancel_synthesis();
352 case TTS_CONFIG_TYPE_VOICE:
354 if (NULL == str_param) {
355 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
359 char* out_lang = NULL;
363 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
364 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
365 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
367 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
369 /* Current language is not available */
370 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
372 if (NULL != out_lang) {
379 case TTS_CONFIG_TYPE_SPEED:
381 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
382 /* set default speed */
384 ret = ttsd_engine_agent_set_default_speed(int_param);
386 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
392 case TTS_CONFIG_TYPE_PITCH:
394 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
395 /* set default speed */
397 ret = ttsd_engine_agent_set_default_pitch(int_param);
399 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
409 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
411 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
412 ttsd_server_finalize(uid);
416 Eina_Bool ttsd_terminate_daemon(void *data)
418 ttsd_data_foreach_clients(__terminate_client, NULL);
422 void __screen_reader_changed_cb(bool value)
424 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
425 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
426 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
428 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
436 int ttsd_initialize(ttse_request_callback_s *callback)
438 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
440 if (ttsd_config_initialize(__config_changed_cb)) {
441 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
445 if (ttsd_player_init()) {
446 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
447 return TTSD_ERROR_OPERATION_FAILED;
450 /* Engine Agent initialize */
451 if (0 != ttsd_engine_agent_init()) {
452 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
453 return TTSD_ERROR_OPERATION_FAILED;
456 /* set current engine */
457 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
458 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
459 // g_is_engine = false;
461 // g_is_engine = true;
463 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
464 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
465 return TTSD_ERROR_OPERATION_FAILED;
468 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
470 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
471 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
474 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
475 if (NULL == g_check_client_timer) {
476 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
479 return TTSD_ERROR_NONE;
484 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
487 if (0 < g_list_length(g_proc_list)) {
488 iter = g_list_first(g_proc_list);
489 while (NULL != iter) {
490 g_proc_list = g_list_remove_link(g_proc_list, iter);
492 iter = g_list_first(g_proc_list);
496 ttsd_config_finalize();
498 ttsd_player_release();
500 ttsd_engine_agent_release();
502 if (NULL != g_check_client_timer) {
503 ecore_timer_del(g_check_client_timer);
504 g_check_client_timer = NULL;
506 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
509 return TTSD_ERROR_NONE;
513 * TTS Server Functions for Client
516 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
518 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
520 if (-1 != ttsd_data_is_client(uid)) {
521 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
522 return TTSD_ERROR_NONE;
525 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
526 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
527 return TTSD_ERROR_OPERATION_FAILED;
529 if (0 != ttsd_data_new_client(pid, uid)) {
530 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
531 return TTSD_ERROR_OPERATION_FAILED;
534 if (0 != ttsd_player_create_instance(uid)) {
535 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
536 return TTSD_ERROR_OPERATION_FAILED;
539 return TTSD_ERROR_NONE;
542 static Eina_Bool __quit_ecore_loop(void *data)
544 ttsd_dbus_close_connection();
545 ttsd_network_finalize();
547 ecore_main_loop_quit();
553 static void __read_proc()
556 struct dirent *dirp = NULL;
560 if (0 < g_list_length(g_proc_list)) {
561 iter = g_list_first(g_proc_list);
562 while (NULL != iter) {
563 g_proc_list = g_list_remove_link(g_proc_list, iter);
565 iter = g_list_first(g_proc_list);
569 dp = opendir("/proc");
571 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
577 tmp = atoi(dirp->d_name);
578 if (0 >= tmp) continue;
579 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
581 } while (NULL != dirp);
587 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
593 for (i = 0; i < g_list_length(g_proc_list); i++) {
594 iter = g_list_nth(g_proc_list, i);
596 if (pid == GPOINTER_TO_INT(iter->data)) {
597 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
604 if (false == exist) {
605 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
606 ttsd_server_finalize(uid);
611 char appid[128] = {0, };
612 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
613 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
616 if (0 < strlen(appid)) {
617 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
619 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
622 result = ttsdc_send_hello(pid, uid);
625 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
626 ttsd_server_finalize(uid);
627 } else if (-1 == result) {
628 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
636 Eina_Bool ttsd_cleanup_client(void *data)
638 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
641 if (0 < ttsd_data_get_client_count()) {
642 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
644 ecore_timer_add(0, __quit_ecore_loop, NULL);
647 SLOG(LOG_DEBUG, tts_tag(), "@@@");
652 void __used_voice_cb(const char* lang, int type)
654 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
655 if (0 != ttsd_engine_unload_voice(lang, type)) {
656 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
660 int ttsd_server_finalize(int uid)
662 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
665 if (0 > ttsd_data_get_client_state(uid, &state)) {
666 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
669 ttsd_server_stop(uid);
670 ttsd_player_stop(uid);
672 ttsd_player_destroy_instance(uid);
674 /* Need to unload voice when used voice is unregistered */
675 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
676 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
677 return TTSD_ERROR_OPERATION_FAILED;
680 ttsd_data_delete_client(uid);
682 /* unload engine, if ref count of client is 0 */
683 if (0 == ttsd_data_get_client_count()) {
684 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
685 ecore_timer_add(0, __quit_ecore_loop, NULL);
688 return TTSD_ERROR_NONE;
691 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
694 if (0 > ttsd_data_get_client_state(uid, &state)) {
695 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
696 return TTSD_ERROR_INVALID_PARAMETER;
699 /* check valid voice */
700 char* temp_lang = NULL;
702 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
703 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
704 if (NULL != temp_lang) {
708 return TTSD_ERROR_INVALID_VOICE;
711 if (NULL == temp_lang) {
712 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
713 return TTSD_ERROR_INVALID_VOICE;
716 speak_data_s* speak_data = NULL;
717 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
718 if (NULL == speak_data) {
719 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
720 if (NULL != temp_lang) {
724 return TTSD_ERROR_OPERATION_FAILED;
727 speak_data->lang = strdup(lang);
728 speak_data->vctype = voice_type;
730 speak_data->speed = speed;
731 speak_data->utt_id = utt_id;
733 speak_data->text = strdup(text);
735 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);
737 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
739 ret = ttsd_data_add_speak_data(uid, speak_data);
741 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
742 if (NULL != temp_lang) {
746 if (NULL != speak_data) {
747 if (NULL != speak_data->lang) free(speak_data->lang);
748 if (NULL != speak_data->text) free(speak_data->text);
750 speak_data->lang = NULL;
751 speak_data->text = NULL;
760 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
761 /* Request load voice */
762 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
763 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
764 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
768 if (NULL != temp_lang) {
773 if (APP_STATE_PLAYING == state) {
774 /* check if engine use network */
775 if (ttsd_engine_agent_need_network()) {
776 if (false == ttsd_network_is_connected()) {
777 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
778 return TTSD_ERROR_OPERATION_FAILED;
782 /* Check whether tts-engine is running or not */
783 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
784 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
786 __synthesis(uid, credential);
790 return TTSD_ERROR_NONE;
793 Eina_Bool __send_interrupt_client(void *data)
795 intptr_t puid = (intptr_t)data;
798 int pid = ttsd_data_get_pid(uid);
800 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
801 /* send message to client about changing state */
802 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
804 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
810 int ttsd_server_play(int uid, const char* credential)
813 if (0 > ttsd_data_get_client_state(uid, &state)) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
815 return TTSD_ERROR_INVALID_PARAMETER;
818 if (APP_STATE_PLAYING == state) {
819 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
820 return TTSD_ERROR_NONE;
823 /* check if engine use network */
824 if (ttsd_engine_agent_need_network()) {
825 if (false == ttsd_network_is_connected()) {
826 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
827 return TTSD_ERROR_OUT_OF_NETWORK;
831 int current_uid = ttsd_data_get_current_playing();
832 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
834 if (uid != current_uid && -1 != current_uid) {
835 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
836 /* Send interrupt message */
837 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
840 if (0 != ttsd_server_stop(current_uid)) {
841 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
843 if (0 != ttsd_player_stop(current_uid)) {
844 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
847 intptr_t pcurrent_uid = (intptr_t)current_uid;
848 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
850 /* Default mode policy of interrupt is "Pause" */
852 /* Send interrupt message */
853 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
856 if (0 != ttsd_player_pause(current_uid)) {
857 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
861 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
863 intptr_t pcurrent_uid = (intptr_t)current_uid;
864 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
868 /* Change current play */
869 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
870 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
871 return TTSD_ERROR_OPERATION_FAILED;
874 if (APP_STATE_PAUSED == state) {
875 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
878 if (0 != ttsd_player_resume(uid)) {
879 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
883 /* Check whether tts-engine is running or not */
884 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
885 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
887 __synthesis(uid, credential);
890 return TTSD_ERROR_NONE;
893 int ttsd_server_stop(int uid)
896 if (0 > ttsd_data_get_client_state(uid, &state)) {
897 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
898 return TTSD_ERROR_INVALID_PARAMETER;
901 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
903 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
904 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
905 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
908 ret = ttsd_engine_cancel_synthesis();
910 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
913 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
915 if (0 != ttsd_player_clear(uid))
916 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
918 ttsd_data_set_client_state(uid, APP_STATE_READY);
922 ttsd_data_clear_data(uid);
924 return TTSD_ERROR_NONE;
927 int ttsd_server_pause(int uid, int* utt_id)
930 if (0 > ttsd_data_get_client_state(uid, &state)) {
931 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
932 return TTSD_ERROR_INVALID_PARAMETER;
935 if (APP_STATE_PLAYING != state) {
936 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
937 return TTSD_ERROR_INVALID_STATE;
940 *utt_id = g_utt.uttid;
941 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
944 ret = ttsd_player_pause(uid);
946 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
947 return TTSD_ERROR_OPERATION_FAILED;
950 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
952 return TTSD_ERROR_NONE;
955 int ttsd_server_get_support_voices(int uid, GList** voice_list)
958 if (0 > ttsd_data_get_client_state(uid, &state)) {
959 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
960 return TTSD_ERROR_INVALID_PARAMETER;
964 int ret = ttsd_engine_get_voice_list(voice_list);
966 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
970 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
972 return TTSD_ERROR_NONE;
975 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
978 if (0 > ttsd_data_get_client_state(uid, &state)) {
979 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
980 return TTSD_ERROR_INVALID_PARAMETER;
983 /* get current voice */
984 int ret = ttsd_engine_get_default_voice(language, voice_type);
986 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
990 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
992 return TTSD_ERROR_NONE;
995 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
998 if (0 > ttsd_data_get_client_state(uid, &state)) {
999 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1000 return TTSD_ERROR_INVALID_PARAMETER;
1003 if (APP_STATE_READY != state) {
1004 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1005 return TTSD_ERROR_INVALID_STATE;
1008 int ret = ttsd_engine_set_private_data(key, data);
1010 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1012 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1018 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1021 if (0 > ttsd_data_get_client_state(uid, &state)) {
1022 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1023 return TTSD_ERROR_INVALID_PARAMETER;
1026 if (APP_STATE_READY != state) {
1027 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1028 return TTSD_ERROR_INVALID_STATE;
1031 int ret = ttsd_engine_get_private_data(key, data);
1033 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1035 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1041 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1043 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1046 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1048 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1054 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1056 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1059 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1061 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1067 int ttsd_server_play_pcm(int uid)
1070 if (0 > ttsd_data_get_client_state(uid, &state)) {
1071 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1072 return TTSD_ERROR_INVALID_PARAMETER;
1075 if (APP_STATE_PLAYING == state) {
1076 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1077 return TTSD_ERROR_NONE;
1080 int current_uid = ttsd_data_get_current_playing();
1081 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1083 if (uid != current_uid && -1 != current_uid) {
1084 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1085 /* Send interrupt message */
1086 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1089 if (0 != ttsd_server_stop(current_uid)) {
1090 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1092 if (0 != ttsd_player_stop(current_uid)) {
1093 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1096 intptr_t pcurrent_uid = (intptr_t)current_uid;
1097 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1099 /* Default mode policy of interrupt is "Pause" */
1101 /* Send interrupt message */
1102 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1105 if (0 != ttsd_player_pause(current_uid)) {
1106 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1110 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1112 intptr_t pcurrent_uid = (intptr_t)current_uid;
1113 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1117 /* Change current play */
1118 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1119 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1120 return TTSD_ERROR_OPERATION_FAILED;
1123 return TTSD_ERROR_NONE;
1126 int ttsd_server_stop_pcm(int uid)
1129 if (0 > ttsd_data_get_client_state(uid, &state)) {
1130 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1131 return TTSD_ERROR_INVALID_PARAMETER;
1134 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1136 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1137 if (0 != ttsd_player_clear(uid))
1138 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1140 ttsd_data_set_client_state(uid, APP_STATE_READY);
1143 /* Reset all data */
1144 ttsd_data_clear_data(uid);
1146 return TTSD_ERROR_NONE;
1149 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1151 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1155 /* Synthesis is success */
1156 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1157 if (TTSE_RESULT_EVENT_START == event) {
1158 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1159 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1160 uid, uttid, data, data_size, audio_type, rate);
1161 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1162 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1163 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1164 uid, uttid, data, data_size, audio_type, rate);
1166 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1169 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1170 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1171 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1172 return TTSD_ERROR_INVALID_PARAMETER;
1176 sound_data_s* temp_sound_data = NULL;
1177 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1178 if (NULL == temp_sound_data) {
1179 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1180 return TTSD_ERROR_OUT_OF_MEMORY;
1183 temp_sound_data->data = NULL;
1184 temp_sound_data->rate = 0;
1185 temp_sound_data->data_size = 0;
1187 if (0 < data_size) {
1188 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1189 if (NULL != temp_sound_data->data) {
1190 memcpy(temp_sound_data->data, data, data_size);
1191 temp_sound_data->data_size = data_size;
1192 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1193 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1195 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1198 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1201 temp_sound_data->utt_id = uttid;
1202 temp_sound_data->event = event;
1203 temp_sound_data->audio_type = audio_type;
1204 temp_sound_data->rate = rate;
1206 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1207 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1209 if (NULL != temp_sound_data->data) {
1210 free(temp_sound_data->data);
1211 temp_sound_data->data = NULL;
1214 free(temp_sound_data);
1215 temp_sound_data = NULL;
1218 if (0 != ttsd_player_play(uid)) {
1219 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1221 /* Change ready state */
1222 ttsd_server_stop(uid);
1225 tmp_pid = ttsd_data_get_pid(uid);
1226 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1229 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1232 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1234 return TTSD_ERROR_NONE;