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.
14 #include <app_manager.h>
18 #include "ttsd_config.h"
19 #include "ttsd_data.h"
20 #include "ttsd_dbus.h"
21 #include "ttsd_dbus_server.h"
22 #include "ttsd_engine_agent.h"
23 #include "ttsd_main.h"
24 #include "ttsd_network.h"
25 #include "ttsd_player.h"
26 #include "ttsd_server.h"
29 #define CLIENT_CLEAN_UP_TIME 500
33 TTSD_SYNTHESIS_CONTROL_DOING = 0,
34 TTSD_SYNTHESIS_CONTROL_DONE = 1,
35 TTSD_SYNTHESIS_CONTROL_EXPIRED = 2
36 } ttsd_synthesis_control_e;
43 /* If current engine exist */
44 //static bool g_is_engine;
46 /* If engine is running */
47 static ttsd_synthesis_control_e g_synth_control;
49 static Ecore_Timer* g_check_client_timer = NULL;
50 static Ecore_Timer* g_wait_timer = NULL;
52 static utterance_t g_utt;
54 static GList *g_proc_list = NULL;
56 /* Function definitions */
57 static int __synthesis(int uid, const char* credential);
59 static int __server_set_synth_control(ttsd_synthesis_control_e control)
61 g_synth_control = control;
65 static ttsd_synthesis_control_e __server_get_synth_control()
67 return g_synth_control;
70 static Eina_Bool __wait_synthesis(void *data)
72 /* get current play */
73 char* credential = (char*)data;
74 int uid = ttsd_data_get_current_playing();
77 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
81 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
82 /* Start next synthesis */
83 __synthesis(uid, credential);
93 static int __synthesis(int uid, const char* credential)
95 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
97 speak_data_s* speak_data = NULL;
98 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
99 if (NULL == speak_data) {
100 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
104 int pid = ttsd_data_get_pid(uid);
105 char appid[1024] = {0, };
106 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
107 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
110 if (NULL == speak_data->lang || NULL == speak_data->text) {
111 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
112 ttsd_server_stop(uid);
114 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
116 ttsd_data_clear_speak_data(&speak_data);
122 g_utt.uttid = speak_data->utt_id;
124 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
125 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
126 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
127 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
128 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
129 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
132 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
133 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
135 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
137 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
139 ttsd_server_stop(uid);
141 int pid = ttsd_data_get_pid(uid);
142 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
144 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
147 ttsd_data_clear_speak_data(&speak_data);
149 ttsd_data_clear_speak_data(&speak_data);
152 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
158 * TTS Server Callback Functions
160 int ttsd_send_error(ttse_error_e error, const char* msg)
163 int uttid = g_utt.uttid;
164 int tmp_pid = ttsd_data_get_pid(uid);
166 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));
168 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
170 if (0 != ttsd_player_clear(uid))
171 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
173 if (0 != ttsd_data_clear_data(uid))
174 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
176 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
177 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
179 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
180 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
182 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
188 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)
190 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
193 int uttid = g_utt.uttid;
195 /* Synthesis is success */
196 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
197 if (TTSE_RESULT_EVENT_START == event) {
198 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
199 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
200 uid, uttid, data, data_size, audio_type, rate);
201 } else if (TTSE_RESULT_EVENT_FINISH == event) {
202 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
203 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
204 uid, uttid, data, data_size, audio_type, rate);
206 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
210 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
211 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
212 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
213 SLOG(LOG_DEBUG, tts_tag(), "@@@");
214 return TTSD_ERROR_OPERATION_FAILED;
217 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
218 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
219 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
220 SLOG(LOG_DEBUG, tts_tag(), "@@@");
221 return TTSD_ERROR_INVALID_PARAMETER;
225 sound_data_s* temp_sound_data = NULL;
226 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
227 if (NULL == temp_sound_data) {
228 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
229 return TTSD_ERROR_OUT_OF_MEMORY;
232 temp_sound_data->data = NULL;
233 temp_sound_data->rate = 0;
234 temp_sound_data->data_size = 0;
237 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
238 if (NULL != temp_sound_data->data) {
239 memcpy(temp_sound_data->data, data, data_size);
240 temp_sound_data->data_size = data_size;
241 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
242 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
244 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
247 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
250 temp_sound_data->utt_id = uttid;
251 temp_sound_data->event = event;
252 temp_sound_data->audio_type = audio_type;
253 temp_sound_data->rate = rate;
255 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
256 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
259 if (event == TTSE_RESULT_EVENT_FINISH) {
260 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
263 if (0 != ttsd_player_play(uid)) {
264 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
266 /* Change ready state */
267 ttsd_server_stop(uid);
270 tmp_pid = ttsd_data_get_pid(uid);
271 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
274 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
275 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
279 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
280 SLOG(LOG_DEBUG, tts_tag(), " ");*/
282 return TTSD_ERROR_NONE;
285 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
287 /* clear client data */
288 ttsd_data_clear_data(uid);
289 ttsd_data_set_client_state(uid, APP_STATE_READY);
292 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
294 ttsd_data_delete_client(uid);
300 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
303 case TTS_CONFIG_TYPE_ENGINE:
305 /* TODO - Determine the policy when engine process get engine changed cb */
306 if (NULL == str_param) {
307 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
312 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
313 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
314 ret = ttsd_engine_agent_unload_current_engine();
316 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
319 ret = ttsd_engine_agent_load_current_engine();
321 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
326 /* stop all player */
327 ttsd_player_all_stop();
329 /* send interrupt message to all clients */
330 ttsd_data_foreach_clients(__get_client_cb, NULL);
332 ttsd_engine_cancel_synthesis();
337 case TTS_CONFIG_TYPE_VOICE:
339 if (NULL == str_param) {
340 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
344 char* out_lang = NULL;
348 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
349 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
350 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
352 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
354 /* Current language is not available */
355 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
357 if (NULL != out_lang) {
364 case TTS_CONFIG_TYPE_SPEED:
366 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
367 /* set default speed */
369 ret = ttsd_engine_agent_set_default_speed(int_param);
371 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
377 case TTS_CONFIG_TYPE_PITCH:
379 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
380 /* set default speed */
382 ret = ttsd_engine_agent_set_default_pitch(int_param);
384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
394 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
396 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
397 ttsd_server_finalize(uid);
401 Eina_Bool ttsd_terminate_daemon(void *data)
403 ttsd_data_foreach_clients(__terminate_client, NULL);
407 void __screen_reader_changed_cb(bool value)
409 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
410 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
411 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
413 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
421 int ttsd_initialize(ttse_request_callback_s *callback)
423 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
425 if (ttsd_config_initialize(__config_changed_cb)) {
426 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
430 if (ttsd_player_init()) {
431 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
432 return TTSD_ERROR_OPERATION_FAILED;
435 /* Engine Agent initialize */
436 if (0 != ttsd_engine_agent_init()) {
437 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
438 return TTSD_ERROR_OPERATION_FAILED;
441 /* set current engine */
442 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
443 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
444 // g_is_engine = false;
446 // g_is_engine = true;
448 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
449 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
450 return TTSD_ERROR_OPERATION_FAILED;
453 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
455 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
456 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
459 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
460 if (NULL == g_check_client_timer) {
461 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
464 return TTSD_ERROR_NONE;
469 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
472 if (0 < g_list_length(g_proc_list)) {
473 iter = g_list_first(g_proc_list);
474 while (NULL != iter) {
475 g_proc_list = g_list_remove_link(g_proc_list, iter);
477 iter = g_list_first(g_proc_list);
481 ttsd_config_finalize();
483 ttsd_player_release();
485 ttsd_engine_agent_release();
487 if (NULL != g_check_client_timer) {
488 ecore_timer_del(g_check_client_timer);
489 g_check_client_timer = NULL;
491 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
494 return TTSD_ERROR_NONE;
498 * TTS Server Functions for Client
501 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
503 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
505 if (-1 != ttsd_data_is_client(uid)) {
506 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
507 return TTSD_ERROR_NONE;
510 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
511 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
512 return TTSD_ERROR_OPERATION_FAILED;
515 if (true == *credential_needed) {
517 if (0 != app_manager_get_app_id(pid, &appid)) {
518 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
520 bool is_agreed = false;
521 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
522 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
525 return TTSD_ERROR_OPERATION_FAILED;
530 if (false == is_agreed) {
531 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
532 return TTSD_ERROR_PERMISSION_DENIED;
536 if (0 != ttsd_data_new_client(pid, uid)) {
537 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
538 return TTSD_ERROR_OPERATION_FAILED;
541 if (0 != ttsd_player_create_instance(uid)) {
542 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
543 return TTSD_ERROR_OPERATION_FAILED;
546 return TTSD_ERROR_NONE;
549 static Eina_Bool __quit_ecore_loop(void *data)
551 ttsd_dbus_close_connection();
552 ttsd_network_finalize();
554 ecore_main_loop_quit();
560 static void __read_proc()
563 struct dirent *dirp = NULL;
567 if (0 < g_list_length(g_proc_list)) {
568 iter = g_list_first(g_proc_list);
569 while (NULL != iter) {
570 g_proc_list = g_list_remove_link(g_proc_list, iter);
572 iter = g_list_first(g_proc_list);
576 dp = opendir("/proc");
578 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
584 tmp = atoi(dirp->d_name);
585 if (0 >= tmp) continue;
586 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
588 } while (NULL != dirp);
594 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
600 for (i = 0; i < g_list_length(g_proc_list); i++) {
601 iter = g_list_nth(g_proc_list, i);
603 if (pid == GPOINTER_TO_INT(iter->data)) {
604 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
611 if (false == exist) {
612 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
613 ttsd_server_finalize(uid);
618 char appid[1024] = {0, };
619 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
620 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
623 if (0 < strlen(appid)) {
624 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
626 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
629 result = ttsdc_send_hello(pid, uid);
632 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
633 ttsd_server_finalize(uid);
634 } else if (-1 == result) {
635 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
643 Eina_Bool ttsd_cleanup_client(void *data)
645 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
648 if (0 < ttsd_data_get_client_count()) {
649 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
651 ecore_timer_add(0, __quit_ecore_loop, NULL);
654 SLOG(LOG_DEBUG, tts_tag(), "@@@");
659 void __used_voice_cb(const char* lang, int type)
661 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
662 if (0 != ttsd_engine_unload_voice(lang, type)) {
663 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
667 int ttsd_server_finalize(int uid)
669 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
671 app_tts_state_e state;
672 if (0 > ttsd_data_get_client_state(uid, &state)) {
673 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
676 ttsd_server_stop(uid);
677 ttsd_player_stop(uid);
679 ttsd_player_destroy_instance(uid);
681 /* Need to unload voice when used voice is unregistered */
682 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
683 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
684 return TTSD_ERROR_OPERATION_FAILED;
687 ttsd_data_delete_client(uid);
689 /* unload engine, if ref count of client is 0 */
690 if (0 == ttsd_data_get_client_count()) {
691 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
692 ecore_timer_add(0, __quit_ecore_loop, NULL);
695 return TTSD_ERROR_NONE;
698 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
700 app_tts_state_e state;
701 if (0 > ttsd_data_get_client_state(uid, &state)) {
702 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
703 return TTSD_ERROR_INVALID_PARAMETER;
706 /* check valid voice */
707 char* temp_lang = NULL;
709 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
710 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
711 if (NULL != temp_lang) {
715 return TTSD_ERROR_INVALID_VOICE;
718 if (NULL == temp_lang) {
719 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
720 return TTSD_ERROR_INVALID_VOICE;
723 speak_data_s* speak_data = NULL;
724 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
725 if (NULL == speak_data) {
726 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
727 if (NULL != temp_lang) {
731 return TTSD_ERROR_OPERATION_FAILED;
734 speak_data->lang = strdup(lang);
735 speak_data->vctype = voice_type;
737 speak_data->speed = speed;
738 speak_data->utt_id = utt_id;
740 speak_data->text = strdup(text);
742 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);
744 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
746 ret = ttsd_data_add_speak_data(uid, speak_data);
748 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
749 if (NULL != temp_lang) {
753 if (NULL != speak_data) {
754 if (NULL != speak_data->lang) free(speak_data->lang);
755 if (NULL != speak_data->text) free(speak_data->text);
757 speak_data->lang = NULL;
758 speak_data->text = NULL;
767 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
768 /* Request load voice */
769 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
770 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
771 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
775 if (NULL != temp_lang) {
780 if (APP_STATE_PLAYING == state) {
781 /* check if engine use network */
782 if (ttsd_engine_agent_need_network()) {
783 if (false == ttsd_network_is_connected()) {
784 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
785 return TTSD_ERROR_OPERATION_FAILED;
789 /* Check whether tts-engine is running or not */
790 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
791 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
793 __synthesis(uid, credential);
797 return TTSD_ERROR_NONE;
800 Eina_Bool __send_interrupt_client(void *data)
802 intptr_t puid = (intptr_t)data;
805 int pid = ttsd_data_get_pid(uid);
807 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
808 /* send message to client about changing state */
809 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
811 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
817 int ttsd_server_play(int uid, const char* credential)
819 app_tts_state_e state;
820 if (0 > ttsd_data_get_client_state(uid, &state)) {
821 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
822 return TTSD_ERROR_INVALID_PARAMETER;
825 if (APP_STATE_PLAYING == state) {
826 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
827 return TTSD_ERROR_NONE;
830 /* check if engine use network */
831 if (ttsd_engine_agent_need_network()) {
832 if (false == ttsd_network_is_connected()) {
833 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
834 return TTSD_ERROR_OUT_OF_NETWORK;
838 /* check the current playback focus */
839 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
840 bool is_current_interrupt = false;
841 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
842 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
844 if (true == is_current_interrupt) {
845 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
846 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
851 int current_uid = ttsd_data_get_current_playing();
852 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
854 if (uid != current_uid && -1 != current_uid) {
855 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
856 /* Send interrupt message */
857 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
860 if (0 != ttsd_server_stop(current_uid)) {
861 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
863 if (0 != ttsd_player_stop(current_uid)) {
864 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
867 intptr_t pcurrent_uid = (intptr_t)current_uid;
868 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
870 /* Default mode policy of interrupt is "Pause" */
872 /* Send interrupt message */
873 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
876 if (0 != ttsd_player_pause(current_uid)) {
877 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
881 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
883 intptr_t pcurrent_uid = (intptr_t)current_uid;
884 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
888 /* Change current play */
889 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
890 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
891 return TTSD_ERROR_OPERATION_FAILED;
894 if (APP_STATE_PAUSED == state) {
895 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
898 if (0 != ttsd_player_resume(uid)) {
899 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
903 /* Check whether tts-engine is running or not */
904 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
905 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
907 __synthesis(uid, credential);
910 return TTSD_ERROR_NONE;
913 int ttsd_server_stop(int uid)
915 app_tts_state_e state;
916 if (0 > ttsd_data_get_client_state(uid, &state)) {
917 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
918 return TTSD_ERROR_INVALID_PARAMETER;
921 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
923 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
924 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
925 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
928 ret = ttsd_engine_cancel_synthesis();
930 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
933 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
935 if (0 != ttsd_player_clear(uid))
936 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
938 ttsd_data_set_client_state(uid, APP_STATE_READY);
942 ttsd_data_clear_data(uid);
944 return TTSD_ERROR_NONE;
947 int ttsd_server_pause(int uid, int* utt_id)
949 app_tts_state_e state;
950 if (0 > ttsd_data_get_client_state(uid, &state)) {
951 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
952 return TTSD_ERROR_INVALID_PARAMETER;
955 if (APP_STATE_PLAYING != state) {
956 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
957 return TTSD_ERROR_INVALID_STATE;
960 *utt_id = g_utt.uttid;
961 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
964 ret = ttsd_player_pause(uid);
966 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
967 return TTSD_ERROR_OPERATION_FAILED;
970 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
972 return TTSD_ERROR_NONE;
975 int ttsd_server_get_support_voices(int uid, GList** voice_list)
977 app_tts_state_e state;
978 if (0 > ttsd_data_get_client_state(uid, &state)) {
979 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
980 return TTSD_ERROR_INVALID_PARAMETER;
984 int ret = ttsd_engine_get_voice_list(voice_list);
986 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
990 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
992 return TTSD_ERROR_NONE;
995 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
997 app_tts_state_e state;
998 if (0 > ttsd_data_get_client_state(uid, &state)) {
999 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1000 return TTSD_ERROR_INVALID_PARAMETER;
1003 /* get current voice */
1004 int ret = ttsd_engine_get_default_voice(language, voice_type);
1006 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1010 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1012 return TTSD_ERROR_NONE;
1015 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1017 app_tts_state_e state;
1018 if (0 > ttsd_data_get_client_state(uid, &state)) {
1019 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1020 return TTSD_ERROR_INVALID_PARAMETER;
1023 if (APP_STATE_READY != state) {
1024 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1025 return TTSD_ERROR_INVALID_STATE;
1028 int ret = ttsd_engine_set_private_data(key, data);
1030 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1032 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1038 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1040 app_tts_state_e state;
1041 if (0 > ttsd_data_get_client_state(uid, &state)) {
1042 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1043 return TTSD_ERROR_INVALID_PARAMETER;
1046 if (APP_STATE_READY != state) {
1047 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1048 return TTSD_ERROR_INVALID_STATE;
1051 int ret = ttsd_engine_get_private_data(key, data);
1053 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1055 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1061 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1063 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1066 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1068 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1074 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1076 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1079 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1081 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1087 int ttsd_server_play_pcm(int uid)
1089 app_tts_state_e state;
1090 if (0 > ttsd_data_get_client_state(uid, &state)) {
1091 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1092 return TTSD_ERROR_INVALID_PARAMETER;
1095 if (APP_STATE_PLAYING == state) {
1096 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1097 return TTSD_ERROR_NONE;
1100 int current_uid = ttsd_data_get_current_playing();
1101 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1103 if (uid != current_uid && -1 != current_uid) {
1104 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1105 /* Send interrupt message */
1106 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1109 if (0 != ttsd_server_stop(current_uid)) {
1110 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1112 if (0 != ttsd_player_stop(current_uid)) {
1113 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1116 intptr_t pcurrent_uid = (intptr_t)current_uid;
1117 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1119 /* Default mode policy of interrupt is "Pause" */
1121 /* Send interrupt message */
1122 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1125 if (0 != ttsd_player_pause(current_uid)) {
1126 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1130 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1132 intptr_t pcurrent_uid = (intptr_t)current_uid;
1133 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1137 /* Change current play */
1138 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1139 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1140 return TTSD_ERROR_OPERATION_FAILED;
1143 if (APP_STATE_PAUSED == state) {
1144 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1147 if (0 != ttsd_player_resume(uid)) {
1148 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1151 if (0 != ttsd_player_play_pcm(uid)) {
1152 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1154 // Change ready state
1155 ttsd_server_stop_pcm(uid);
1158 tmp_pid = ttsd_data_get_pid(uid);
1159 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1163 return TTSD_ERROR_NONE;
1166 int ttsd_server_stop_pcm(int uid)
1168 app_tts_state_e state;
1169 if (0 > ttsd_data_get_client_state(uid, &state)) {
1170 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1171 return TTSD_ERROR_INVALID_PARAMETER;
1174 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1176 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1177 ttsd_data_set_client_state(uid, APP_STATE_READY);
1180 /* Reset all data */
1181 ttsd_data_clear_data(uid);
1183 ttsd_player_stop(uid);
1186 return TTSD_ERROR_NONE;
1189 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1191 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1195 /* Synthesis is success */
1196 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1197 if (TTSE_RESULT_EVENT_START == event) {
1198 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1199 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1200 uid, uttid, data, data_size, audio_type, rate);
1201 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1202 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1203 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1204 uid, uttid, data, data_size, audio_type, rate);
1206 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1209 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1210 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1211 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1212 return TTSD_ERROR_INVALID_PARAMETER;
1216 sound_data_s* temp_sound_data = NULL;
1217 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1218 if (NULL == temp_sound_data) {
1219 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1220 return TTSD_ERROR_OUT_OF_MEMORY;
1223 temp_sound_data->data = NULL;
1224 temp_sound_data->rate = 0;
1225 temp_sound_data->data_size = 0;
1227 if (0 < data_size) {
1228 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1229 if (NULL != temp_sound_data->data) {
1230 memcpy(temp_sound_data->data, data, data_size);
1231 temp_sound_data->data_size = data_size;
1232 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1233 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1235 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1238 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1241 temp_sound_data->utt_id = uttid;
1242 temp_sound_data->event = event;
1243 temp_sound_data->audio_type = audio_type;
1244 temp_sound_data->rate = rate;
1246 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1247 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1249 if (NULL != temp_sound_data->data) {
1250 free(temp_sound_data->data);
1251 temp_sound_data->data = NULL;
1254 free(temp_sound_data);
1255 temp_sound_data = NULL;
1258 /* if (0 != ttsd_player_play(uid)) {
1259 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1261 // Change ready state
1262 ttsd_server_stop(uid);
1265 tmp_pid = ttsd_data_get_pid(uid);
1266 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1270 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1273 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1275 return TTSD_ERROR_NONE;