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, 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();
554 static void __read_proc()
557 struct dirent *dirp = NULL;
561 if (0 < g_list_length(g_proc_list)) {
562 iter = g_list_first(g_proc_list);
563 while (NULL != iter) {
564 g_proc_list = g_list_remove_link(g_proc_list, iter);
566 iter = g_list_first(g_proc_list);
570 dp = opendir("/proc");
572 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
578 tmp = atoi(dirp->d_name);
579 if (0 >= tmp) continue;
580 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
582 } while (NULL != dirp);
588 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
594 for (i = 0; i < g_list_length(g_proc_list); i++) {
595 iter = g_list_nth(g_proc_list, i);
597 if (pid == GPOINTER_TO_INT(iter->data)) {
598 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
605 if (false == exist) {
606 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
607 ttsd_server_finalize(uid);
612 char appid[128] = {0, };
613 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
614 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
617 if (0 < strlen(appid)) {
618 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
620 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
623 result = ttsdc_send_hello(pid, uid);
626 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
627 ttsd_server_finalize(uid);
628 } else if (-1 == result) {
629 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
637 Eina_Bool ttsd_cleanup_client(void *data)
639 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
642 if (0 < ttsd_data_get_client_count()) {
643 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
645 ecore_timer_add(0, __quit_ecore_loop, NULL);
648 SLOG(LOG_DEBUG, tts_tag(), "@@@");
653 void __used_voice_cb(const char* lang, int type)
655 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
656 if (0 != ttsd_engine_unload_voice(lang, type)) {
657 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
661 int ttsd_server_finalize(int uid)
663 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
666 if (0 > ttsd_data_get_client_state(uid, &state)) {
667 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
670 ttsd_server_stop(uid);
671 ttsd_player_stop(uid);
673 ttsd_player_destroy_instance(uid);
675 /* Need to unload voice when used voice is unregistered */
676 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
677 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
678 return TTSD_ERROR_OPERATION_FAILED;
681 ttsd_data_delete_client(uid);
683 /* unload engine, if ref count of client is 0 */
684 if (0 == ttsd_data_get_client_count()) {
685 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
686 ecore_timer_add(0, __quit_ecore_loop, NULL);
689 return TTSD_ERROR_NONE;
692 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
695 if (0 > ttsd_data_get_client_state(uid, &state)) {
696 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
697 return TTSD_ERROR_INVALID_PARAMETER;
700 /* check valid voice */
701 char* temp_lang = NULL;
703 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
704 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
705 if (NULL != temp_lang) {
709 return TTSD_ERROR_INVALID_VOICE;
712 if (NULL == temp_lang) {
713 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
714 return TTSD_ERROR_INVALID_VOICE;
717 speak_data_s* speak_data = NULL;
718 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
719 if (NULL == speak_data) {
720 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
721 if (NULL != temp_lang) {
725 return TTSD_ERROR_OPERATION_FAILED;
728 speak_data->lang = strdup(lang);
729 speak_data->vctype = voice_type;
731 speak_data->speed = speed;
732 speak_data->utt_id = utt_id;
734 speak_data->text = strdup(text);
736 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);
738 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
740 ret = ttsd_data_add_speak_data(uid, speak_data);
742 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
743 if (NULL != temp_lang) {
747 if (NULL != speak_data) {
748 if (NULL != speak_data->lang) free(speak_data->lang);
749 if (NULL != speak_data->text) free(speak_data->text);
751 speak_data->lang = NULL;
752 speak_data->text = NULL;
761 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
762 /* Request load voice */
763 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
764 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
765 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
769 if (NULL != temp_lang) {
774 if (APP_STATE_PLAYING == state) {
775 /* check if engine use network */
776 if (ttsd_engine_agent_need_network()) {
777 if (false == ttsd_network_is_connected()) {
778 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
779 return TTSD_ERROR_OPERATION_FAILED;
783 /* Check whether tts-engine is running or not */
784 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
785 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
787 __synthesis(uid, credential);
791 return TTSD_ERROR_NONE;
794 Eina_Bool __send_interrupt_client(void *data)
796 intptr_t puid = (intptr_t)data;
799 int pid = ttsd_data_get_pid(uid);
801 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
802 /* send message to client about changing state */
803 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
805 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
811 int ttsd_server_play(int uid, const char* credential)
814 if (0 > ttsd_data_get_client_state(uid, &state)) {
815 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
816 return TTSD_ERROR_INVALID_PARAMETER;
819 if (APP_STATE_PLAYING == state) {
820 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
821 return TTSD_ERROR_NONE;
824 /* check if engine use network */
825 if (ttsd_engine_agent_need_network()) {
826 if (false == ttsd_network_is_connected()) {
827 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
828 return TTSD_ERROR_OUT_OF_NETWORK;
832 int current_uid = ttsd_data_get_current_playing();
833 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
835 if (uid != current_uid && -1 != current_uid) {
836 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
837 /* Send interrupt message */
838 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
841 if (0 != ttsd_server_stop(current_uid)) {
842 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
844 if (0 != ttsd_player_stop(current_uid)) {
845 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
848 intptr_t pcurrent_uid = (intptr_t)current_uid;
849 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
851 /* Default mode policy of interrupt is "Pause" */
853 /* Send interrupt message */
854 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
857 if (0 != ttsd_player_pause(current_uid)) {
858 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
862 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
864 intptr_t pcurrent_uid = (intptr_t)current_uid;
865 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
869 /* Change current play */
870 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
871 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
872 return TTSD_ERROR_OPERATION_FAILED;
875 if (APP_STATE_PAUSED == state) {
876 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
879 if (0 != ttsd_player_resume(uid)) {
880 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
884 /* Check whether tts-engine is running or not */
885 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
886 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
888 __synthesis(uid, credential);
891 return TTSD_ERROR_NONE;
894 int ttsd_server_stop(int uid)
897 if (0 > ttsd_data_get_client_state(uid, &state)) {
898 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
899 return TTSD_ERROR_INVALID_PARAMETER;
902 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
904 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
905 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
906 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
909 ret = ttsd_engine_cancel_synthesis();
911 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
914 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
916 if (0 != ttsd_player_clear(uid))
917 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
919 ttsd_data_set_client_state(uid, APP_STATE_READY);
923 ttsd_data_clear_data(uid);
925 return TTSD_ERROR_NONE;
928 int ttsd_server_pause(int uid, int* utt_id)
931 if (0 > ttsd_data_get_client_state(uid, &state)) {
932 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
933 return TTSD_ERROR_INVALID_PARAMETER;
936 if (APP_STATE_PLAYING != state) {
937 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
938 return TTSD_ERROR_INVALID_STATE;
941 *utt_id = g_utt.uttid;
942 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
945 ret = ttsd_player_pause(uid);
947 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
948 return TTSD_ERROR_OPERATION_FAILED;
951 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
953 return TTSD_ERROR_NONE;
956 int ttsd_server_get_support_voices(int uid, GList** voice_list)
959 if (0 > ttsd_data_get_client_state(uid, &state)) {
960 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
961 return TTSD_ERROR_INVALID_PARAMETER;
965 int ret = ttsd_engine_get_voice_list(voice_list);
967 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
971 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
973 return TTSD_ERROR_NONE;
976 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
979 if (0 > ttsd_data_get_client_state(uid, &state)) {
980 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
981 return TTSD_ERROR_INVALID_PARAMETER;
984 /* get current voice */
985 int ret = ttsd_engine_get_default_voice(language, voice_type);
987 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
991 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
993 return TTSD_ERROR_NONE;
996 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
999 if (0 > ttsd_data_get_client_state(uid, &state)) {
1000 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1001 return TTSD_ERROR_INVALID_PARAMETER;
1004 if (APP_STATE_READY != state) {
1005 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1006 return TTSD_ERROR_INVALID_STATE;
1009 int ret = ttsd_engine_set_private_data(key, data);
1011 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1013 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1019 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1022 if (0 > ttsd_data_get_client_state(uid, &state)) {
1023 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1024 return TTSD_ERROR_INVALID_PARAMETER;
1027 if (APP_STATE_READY != state) {
1028 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1029 return TTSD_ERROR_INVALID_STATE;
1032 int ret = ttsd_engine_get_private_data(key, data);
1034 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1036 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1042 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1044 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1047 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1049 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1055 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1057 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1060 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1062 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1068 int ttsd_server_play_pcm(int uid)
1071 if (0 > ttsd_data_get_client_state(uid, &state)) {
1072 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1073 return TTSD_ERROR_INVALID_PARAMETER;
1076 if (APP_STATE_PLAYING == state) {
1077 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1078 return TTSD_ERROR_NONE;
1081 int current_uid = ttsd_data_get_current_playing();
1082 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1084 if (uid != current_uid && -1 != current_uid) {
1085 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1086 /* Send interrupt message */
1087 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1090 if (0 != ttsd_server_stop(current_uid)) {
1091 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1093 if (0 != ttsd_player_stop(current_uid)) {
1094 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1097 intptr_t pcurrent_uid = (intptr_t)current_uid;
1098 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1100 /* Default mode policy of interrupt is "Pause" */
1102 /* Send interrupt message */
1103 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1106 if (0 != ttsd_player_pause(current_uid)) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1111 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1113 intptr_t pcurrent_uid = (intptr_t)current_uid;
1114 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1118 /* Change current play */
1119 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1120 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1121 return TTSD_ERROR_OPERATION_FAILED;
1124 return TTSD_ERROR_NONE;
1127 int ttsd_server_stop_pcm(int uid)
1130 if (0 > ttsd_data_get_client_state(uid, &state)) {
1131 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1132 return TTSD_ERROR_INVALID_PARAMETER;
1135 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1137 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1138 if (0 != ttsd_player_clear(uid))
1139 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1141 ttsd_data_set_client_state(uid, APP_STATE_READY);
1144 /* Reset all data */
1145 ttsd_data_clear_data(uid);
1147 return TTSD_ERROR_NONE;
1150 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1152 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1156 /* Synthesis is success */
1157 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1158 if (TTSE_RESULT_EVENT_START == event) {
1159 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1160 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1161 uid, uttid, data, data_size, audio_type, rate);
1162 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1163 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1164 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1165 uid, uttid, data, data_size, audio_type, rate);
1167 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1170 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1171 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1172 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1173 return TTSD_ERROR_INVALID_PARAMETER;
1177 sound_data_s* temp_sound_data = NULL;
1178 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1179 if (NULL == temp_sound_data) {
1180 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1181 return TTSD_ERROR_OUT_OF_MEMORY;
1184 temp_sound_data->data = NULL;
1185 temp_sound_data->rate = 0;
1186 temp_sound_data->data_size = 0;
1188 if (0 < data_size) {
1189 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1190 if (NULL != temp_sound_data->data) {
1191 memcpy(temp_sound_data->data, data, data_size);
1192 temp_sound_data->data_size = data_size;
1193 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1194 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1196 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1199 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1202 temp_sound_data->utt_id = uttid;
1203 temp_sound_data->event = event;
1204 temp_sound_data->audio_type = audio_type;
1205 temp_sound_data->rate = rate;
1207 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1208 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1210 if (NULL != temp_sound_data->data) {
1211 free(temp_sound_data->data);
1212 temp_sound_data->data = NULL;
1215 free(temp_sound_data);
1216 temp_sound_data = NULL;
1219 if (0 != ttsd_player_play(uid)) {
1220 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1222 /* Change ready state */
1223 ttsd_server_stop(uid);
1226 tmp_pid = ttsd_data_get_pid(uid);
1227 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1230 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1233 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1235 return TTSD_ERROR_NONE;