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
36 /* If current engine exist */
37 //static bool g_is_engine;
39 static Ecore_Timer* g_check_client_timer = NULL;
40 static Ecore_Timer* g_wait_timer = NULL;
41 static Ecore_Timer* g_terminate_timer = NULL;
43 static utterance_t g_utt;
45 static GList *g_proc_list = NULL;
47 static bool g_is_paused;
50 /* Function definitions */
51 static int __synthesis(int uid, const char* credential);
53 static Eina_Bool __wait_synthesis(void *data)
55 /* get current play */
56 char* credential = (char*)data;
57 int uid = ttsd_data_get_current_playing();
60 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
64 if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
65 /* Start next synthesis */
66 __synthesis(uid, credential);
76 static int __synthesis(int uid, const char* credential)
78 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
80 speak_data_s* speak_data = NULL;
81 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
82 if (NULL == speak_data) {
83 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
87 int pid = ttsd_data_get_pid(uid);
88 char appid[1024] = {0, };
89 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
90 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
93 if (NULL == speak_data->lang || NULL == speak_data->text) {
94 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
95 ttsd_server_stop(uid);
97 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
99 ttsd_data_clear_speak_data(uid, &speak_data);
105 g_utt.uttid = speak_data->utt_id;
107 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
108 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
109 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
110 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
111 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
112 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
115 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
116 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
118 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
120 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
122 ttsd_server_stop(uid);
124 int pid = ttsd_data_get_pid(uid);
125 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
127 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
130 ttsd_data_clear_speak_data(uid, &speak_data);
132 ttsd_data_clear_speak_data(uid, &speak_data);
135 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
141 * TTS Server Callback Functions
143 int ttsd_send_error(ttse_error_e error, const char* msg)
146 int uttid = g_utt.uttid;
147 int tmp_pid = ttsd_data_get_pid(uid);
149 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));
151 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
153 if (0 != ttsd_player_clear(uid))
154 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
156 if (0 != ttsd_data_clear_data(uid))
157 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
159 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
160 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
162 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
163 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
165 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
166 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
171 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)
173 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
176 int uttid = g_utt.uttid;
178 /* Synthesis is success */
179 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
180 if (TTSE_RESULT_EVENT_START == event) {
181 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
182 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
183 uid, uttid, data, data_size, audio_type, rate);
184 } else if (TTSE_RESULT_EVENT_FINISH == event) {
185 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
186 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
187 uid, uttid, data, data_size, audio_type, rate);
189 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
193 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
194 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
195 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
196 SLOG(LOG_DEBUG, tts_tag(), "@@@");
197 return TTSD_ERROR_OPERATION_FAILED;
200 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
201 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
202 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
203 SLOG(LOG_DEBUG, tts_tag(), "@@@");
204 return TTSD_ERROR_INVALID_PARAMETER;
208 sound_data_s* temp_sound_data = NULL;
209 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
210 if (NULL == temp_sound_data) {
211 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
212 return TTSD_ERROR_OUT_OF_MEMORY;
215 temp_sound_data->data = NULL;
216 temp_sound_data->rate = 0;
217 temp_sound_data->data_size = 0;
220 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
221 if (NULL != temp_sound_data->data) {
222 memcpy(temp_sound_data->data, data, data_size);
223 temp_sound_data->data_size = data_size;
224 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
225 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
227 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
230 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
233 temp_sound_data->utt_id = uttid;
234 temp_sound_data->event = event;
235 temp_sound_data->audio_type = audio_type;
236 temp_sound_data->rate = rate;
238 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
239 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
240 if (NULL != temp_sound_data->data) {
241 free(temp_sound_data->data);
242 temp_sound_data->data = NULL;
245 free(temp_sound_data);
246 temp_sound_data = NULL;
248 return TTSD_ERROR_OPERATION_FAILED;
251 if (event == TTSE_RESULT_EVENT_FINISH) {
252 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
255 /* If the app state is paused, do not result to play */
256 if (true == g_is_paused) {
257 SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
258 return TTSD_ERROR_NONE;
261 if (0 != ttsd_player_play(uid)) {
262 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
264 /* Change ready state */
265 ttsd_server_stop(uid);
268 tmp_pid = ttsd_data_get_pid(uid);
269 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
272 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
273 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
277 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
278 SLOG(LOG_DEBUG, tts_tag(), " ");*/
280 return TTSD_ERROR_NONE;
283 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
285 /* clear client data */
286 ttsd_data_clear_data(uid);
287 ttsd_data_set_client_state(uid, APP_STATE_READY);
290 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
292 ttsd_data_delete_client(uid);
298 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
301 case TTS_CONFIG_TYPE_ENGINE:
303 /* TODO - Determine the policy when engine process get engine changed cb */
304 if (NULL == str_param) {
305 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
310 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
311 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
312 ret = ttsd_engine_agent_unload_current_engine();
314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
317 ret = ttsd_engine_agent_load_current_engine();
319 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
324 /* stop all player */
325 ttsd_player_all_stop();
327 /* send interrupt message to all clients */
328 ttsd_data_foreach_clients(__get_client_cb, NULL);
330 ttsd_engine_cancel_synthesis();
335 case TTS_CONFIG_TYPE_VOICE:
337 if (NULL == str_param) {
338 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
342 char* out_lang = NULL;
346 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
347 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
348 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
350 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
352 /* Current language is not available */
353 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
355 if (NULL != out_lang) {
362 case TTS_CONFIG_TYPE_SPEED:
364 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
365 /* set default speed */
367 ret = ttsd_engine_agent_set_default_speed(int_param);
369 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
375 case TTS_CONFIG_TYPE_PITCH:
377 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
378 /* set default speed */
380 ret = ttsd_engine_agent_set_default_pitch(int_param);
382 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
388 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
390 if (0.0 <= double_param && double_param <= 1.0) {
391 /* set default bg volume ratio */
393 ret = ttsd_player_set_background_volume_ratio(double_param);
395 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
405 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
407 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
408 ttsd_server_finalize(uid);
412 Eina_Bool ttsd_terminate_daemon(void *data)
414 ttsd_data_foreach_clients(__terminate_client, NULL);
415 g_terminate_timer = NULL;
419 void __screen_reader_changed_cb(bool value)
421 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
422 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
423 if (g_terminate_timer) {
424 ecore_timer_del(g_terminate_timer);
425 g_terminate_timer = NULL;
427 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
429 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
437 int ttsd_initialize(ttse_request_callback_s *callback)
439 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
441 if (ttsd_config_initialize(__config_changed_cb)) {
442 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
446 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
448 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
449 int ret = ttsd_player_set_background_volume_ratio(ratio);
451 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
454 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
457 if (ttsd_player_init()) {
458 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
459 return TTSD_ERROR_OPERATION_FAILED;
462 /* Engine Agent initialize */
463 if (0 != ttsd_engine_agent_init()) {
464 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
465 return TTSD_ERROR_OPERATION_FAILED;
468 /* set current engine */
469 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
470 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
471 // g_is_engine = false;
473 // g_is_engine = true;
475 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
476 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
477 return TTSD_ERROR_OPERATION_FAILED;
480 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
482 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
483 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
486 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
487 if (NULL == g_check_client_timer) {
488 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
491 return TTSD_ERROR_NONE;
496 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
499 if (0 < g_list_length(g_proc_list)) {
500 iter = g_list_first(g_proc_list);
501 while (NULL != iter) {
502 g_proc_list = g_list_remove_link(g_proc_list, iter);
504 iter = g_list_first(g_proc_list);
508 ttsd_config_finalize();
510 ttsd_player_release();
512 ttsd_engine_agent_release();
516 ecore_timer_del(g_wait_timer);
518 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
521 if (g_terminate_timer) {
522 ecore_timer_del(g_terminate_timer);
523 g_terminate_timer = NULL;
524 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
527 if (NULL != g_check_client_timer) {
528 ecore_timer_del(g_check_client_timer);
529 g_check_client_timer = NULL;
530 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
533 return TTSD_ERROR_NONE;
538 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
540 ttsd_terminate_daemon(NULL);
542 ttsd_dbus_close_connection();
543 ttsd_network_finalize();
546 return TTSD_ERROR_NONE;
550 * TTS Server Functions for Client
553 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
555 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
557 if (-1 != ttsd_data_is_client(uid)) {
558 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
559 return TTSD_ERROR_NONE;
562 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
563 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
564 return TTSD_ERROR_OPERATION_FAILED;
567 if (true == *credential_needed) {
569 if (0 != app_manager_get_app_id(pid, &appid)) {
570 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
572 bool is_agreed = false;
573 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
574 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
577 return TTSD_ERROR_OPERATION_FAILED;
582 if (false == is_agreed) {
583 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
584 return TTSD_ERROR_PERMISSION_DENIED;
588 if (0 != ttsd_data_new_client(pid, uid)) {
589 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
590 return TTSD_ERROR_OPERATION_FAILED;
593 if (0 != ttsd_player_create_instance(uid)) {
594 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
595 return TTSD_ERROR_OPERATION_FAILED;
598 return TTSD_ERROR_NONE;
601 static Eina_Bool __quit_ecore_loop(void *data)
603 ttsd_dbus_close_connection();
604 ttsd_network_finalize();
606 ecore_main_loop_quit();
612 static void __read_proc()
615 struct dirent *dirp = NULL;
619 if (0 < g_list_length(g_proc_list)) {
620 iter = g_list_first(g_proc_list);
621 while (NULL != iter) {
622 g_proc_list = g_list_remove_link(g_proc_list, iter);
624 iter = g_list_first(g_proc_list);
628 dp = opendir("/proc");
630 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
636 tmp = atoi(dirp->d_name);
637 if (0 >= tmp) continue;
638 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
640 } while (NULL != dirp);
646 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
652 for (i = 0; i < g_list_length(g_proc_list); i++) {
653 iter = g_list_nth(g_proc_list, i);
655 if (pid == GPOINTER_TO_INT(iter->data)) {
656 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
663 if (false == exist) {
664 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
665 ttsd_server_finalize(uid);
670 char appid[1024] = {0, };
671 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
672 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
675 if (0 < strlen(appid)) {
676 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
678 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
681 result = ttsdc_send_hello(pid, uid);
684 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
685 ttsd_server_finalize(uid);
686 } else if (-1 == result) {
687 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
695 Eina_Bool ttsd_cleanup_client(void *data)
697 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
700 if (0 < ttsd_data_get_client_count()) {
701 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
703 ecore_timer_add(0, __quit_ecore_loop, NULL);
704 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
708 SLOG(LOG_DEBUG, tts_tag(), "@@@");
713 void __used_voice_cb(const char* lang, int type)
715 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
716 if (0 != ttsd_engine_unload_voice(lang, type)) {
717 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
721 int ttsd_server_finalize(int uid)
723 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
725 app_tts_state_e state;
726 if (0 > ttsd_data_get_client_state(uid, &state)) {
727 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
730 ttsd_server_stop(uid);
731 ttsd_player_stop(uid);
733 ttsd_player_destroy_instance(uid);
735 /* Need to unload voice when used voice is unregistered */
736 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
737 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
738 return TTSD_ERROR_OPERATION_FAILED;
741 ttsd_data_delete_client(uid);
743 /* unload engine, if ref count of client is 0 */
744 if (0 == ttsd_data_get_client_count()) {
745 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
746 ecore_timer_add(0, __quit_ecore_loop, NULL);
749 return TTSD_ERROR_NONE;
752 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
754 app_tts_state_e state;
755 if (0 > ttsd_data_get_client_state(uid, &state)) {
756 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
757 return TTSD_ERROR_INVALID_PARAMETER;
760 /* check valid voice */
761 char* temp_lang = NULL;
763 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
764 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
765 if (NULL != temp_lang) {
769 return TTSD_ERROR_INVALID_VOICE;
772 if (NULL == temp_lang) {
773 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
774 return TTSD_ERROR_INVALID_VOICE;
777 speak_data_s* speak_data = NULL;
778 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
779 if (NULL == speak_data) {
780 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
781 if (NULL != temp_lang) {
785 return TTSD_ERROR_OPERATION_FAILED;
788 speak_data->lang = strdup(lang);
789 speak_data->vctype = voice_type;
791 speak_data->speed = speed;
792 speak_data->utt_id = utt_id;
794 speak_data->text = strdup(text);
796 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);
798 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
800 ret = ttsd_data_add_speak_data(uid, speak_data);
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
803 if (NULL != temp_lang) {
807 if (NULL != speak_data) {
808 if (NULL != speak_data->lang) free(speak_data->lang);
809 if (NULL != speak_data->text) free(speak_data->text);
811 speak_data->lang = NULL;
812 speak_data->text = NULL;
821 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
822 /* Request load voice */
823 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
824 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
825 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
829 if (NULL != temp_lang) {
834 if (APP_STATE_PLAYING == state) {
835 /* check if engine use network */
836 if (ttsd_engine_agent_need_network()) {
837 if (false == ttsd_network_is_connected()) {
838 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
839 return TTSD_ERROR_OPERATION_FAILED;
843 /* Check whether tts-engine is running or not */
844 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
845 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
847 __synthesis(uid, credential);
851 return TTSD_ERROR_NONE;
854 Eina_Bool __send_interrupt_client(void *data)
856 intptr_t puid = (intptr_t)data;
859 int pid = ttsd_data_get_pid(uid);
861 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
862 /* send message to client about changing state */
863 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
865 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
871 int ttsd_server_play(int uid, const char* credential)
873 app_tts_state_e state;
874 if (0 > ttsd_data_get_client_state(uid, &state)) {
875 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
876 return TTSD_ERROR_INVALID_PARAMETER;
879 if (APP_STATE_PLAYING == state) {
880 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
881 return TTSD_ERROR_NONE;
884 /* check if engine use network */
885 if (ttsd_engine_agent_need_network()) {
886 if (false == ttsd_network_is_connected()) {
887 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
888 return TTSD_ERROR_OUT_OF_NETWORK;
892 /* check the current playback focus */
893 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
894 bool is_current_interrupt = false;
895 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
896 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
898 if (true == is_current_interrupt) {
899 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
900 ttsd_data_clear_data(uid);
901 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
906 int current_uid = ttsd_data_get_current_playing();
907 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
909 if (uid != current_uid && -1 != current_uid) {
910 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
911 /* Send interrupt message */
912 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
915 if (0 != ttsd_server_stop(current_uid)) {
916 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
918 if (0 != ttsd_player_stop(current_uid)) {
919 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
922 intptr_t pcurrent_uid = (intptr_t)current_uid;
923 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
925 /* Default mode policy of interrupt is "Pause" */
927 /* Send interrupt message */
928 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
931 if (0 != ttsd_player_pause(current_uid)) {
932 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
936 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
938 intptr_t pcurrent_uid = (intptr_t)current_uid;
939 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
943 /* Change current play */
944 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
945 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
946 return TTSD_ERROR_OPERATION_FAILED;
949 if (APP_STATE_PAUSED == state) {
950 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
955 if (0 != ttsd_player_resume(uid)) {
956 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
960 /* Check whether tts-engine is running or not */
961 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
962 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
964 __synthesis(uid, credential);
967 return TTSD_ERROR_NONE;
970 int ttsd_server_stop(int uid)
972 app_tts_state_e state;
973 if (0 > ttsd_data_get_client_state(uid, &state)) {
974 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
975 return TTSD_ERROR_INVALID_PARAMETER;
978 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
980 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
981 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
982 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
985 ret = ttsd_engine_cancel_synthesis();
987 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
990 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
992 if (0 != ttsd_player_clear(uid))
993 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
995 ttsd_data_set_client_state(uid, APP_STATE_READY);
999 ttsd_data_clear_data(uid);
1000 g_is_paused = false;
1002 return TTSD_ERROR_NONE;
1005 int ttsd_server_pause(int uid, int* utt_id)
1007 app_tts_state_e state;
1008 if (0 > ttsd_data_get_client_state(uid, &state)) {
1009 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1010 return TTSD_ERROR_INVALID_PARAMETER;
1013 if (APP_STATE_PLAYING != state) {
1014 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1015 return TTSD_ERROR_INVALID_STATE;
1020 *utt_id = g_utt.uttid;
1021 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1024 ret = ttsd_player_pause(uid);
1026 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1027 return TTSD_ERROR_OPERATION_FAILED;
1030 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1032 return TTSD_ERROR_NONE;
1035 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1037 app_tts_state_e state;
1038 if (0 > ttsd_data_get_client_state(uid, &state)) {
1039 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1040 return TTSD_ERROR_INVALID_PARAMETER;
1044 int ret = ttsd_engine_get_voice_list(voice_list);
1046 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1050 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1052 return TTSD_ERROR_NONE;
1055 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1057 app_tts_state_e state;
1058 if (0 > ttsd_data_get_client_state(uid, &state)) {
1059 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1060 return TTSD_ERROR_INVALID_PARAMETER;
1063 /* get current voice */
1064 int ret = ttsd_engine_get_default_voice(language, voice_type);
1066 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1070 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1072 return TTSD_ERROR_NONE;
1075 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1077 app_tts_state_e state;
1078 if (0 > ttsd_data_get_client_state(uid, &state)) {
1079 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1080 return TTSD_ERROR_INVALID_PARAMETER;
1083 if (APP_STATE_READY != state) {
1084 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1085 return TTSD_ERROR_INVALID_STATE;
1088 int ret = ttsd_engine_set_private_data(key, data);
1090 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1092 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1098 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1100 app_tts_state_e state;
1101 if (0 > ttsd_data_get_client_state(uid, &state)) {
1102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1103 return TTSD_ERROR_INVALID_PARAMETER;
1106 if (APP_STATE_READY != state) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1108 return TTSD_ERROR_INVALID_STATE;
1111 int ret = ttsd_engine_get_private_data(key, data);
1113 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1115 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1121 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1123 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1126 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1128 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1134 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1136 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1139 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1147 int ttsd_server_play_pcm(int uid)
1149 app_tts_state_e state;
1150 if (0 > ttsd_data_get_client_state(uid, &state)) {
1151 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1152 return TTSD_ERROR_INVALID_PARAMETER;
1155 if (APP_STATE_PLAYING == state) {
1156 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1157 return TTSD_ERROR_NONE;
1160 int current_uid = ttsd_data_get_current_playing();
1161 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1163 if (uid != current_uid && -1 != current_uid) {
1164 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1165 /* Send interrupt message */
1166 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1169 if (0 != ttsd_server_stop(current_uid)) {
1170 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1172 if (0 != ttsd_player_stop(current_uid)) {
1173 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1176 intptr_t pcurrent_uid = (intptr_t)current_uid;
1177 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1179 /* Default mode policy of interrupt is "Pause" */
1181 /* Send interrupt message */
1182 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1185 if (0 != ttsd_player_pause(current_uid)) {
1186 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1190 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1192 intptr_t pcurrent_uid = (intptr_t)current_uid;
1193 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1197 /* Change current play */
1198 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1199 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1200 return TTSD_ERROR_OPERATION_FAILED;
1203 if (APP_STATE_PAUSED == state) {
1204 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1207 if (0 != ttsd_player_resume(uid)) {
1208 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1211 if (0 != ttsd_player_play_pcm(uid)) {
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1214 // Change ready state
1215 ttsd_server_stop_pcm(uid);
1218 tmp_pid = ttsd_data_get_pid(uid);
1219 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1223 return TTSD_ERROR_NONE;
1226 int ttsd_server_stop_pcm(int uid)
1228 app_tts_state_e state;
1229 if (0 > ttsd_data_get_client_state(uid, &state)) {
1230 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1231 return TTSD_ERROR_INVALID_PARAMETER;
1234 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1236 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1237 ttsd_data_set_client_state(uid, APP_STATE_READY);
1240 /* Reset all data */
1241 ttsd_data_clear_data(uid);
1243 ttsd_player_stop(uid);
1246 return TTSD_ERROR_NONE;
1249 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1251 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1255 /* Synthesis is success */
1256 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1257 if (TTSE_RESULT_EVENT_START == event) {
1258 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1259 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1260 uid, uttid, data, data_size, audio_type, rate);
1261 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1262 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1263 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1264 uid, uttid, data, data_size, audio_type, rate);
1266 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1269 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1270 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1271 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1272 return TTSD_ERROR_INVALID_PARAMETER;
1276 sound_data_s* temp_sound_data = NULL;
1277 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1278 if (NULL == temp_sound_data) {
1279 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1280 return TTSD_ERROR_OUT_OF_MEMORY;
1283 temp_sound_data->data = NULL;
1284 temp_sound_data->rate = 0;
1285 temp_sound_data->data_size = 0;
1287 if (0 < data_size) {
1288 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1289 if (NULL != temp_sound_data->data) {
1290 memcpy(temp_sound_data->data, data, data_size);
1291 temp_sound_data->data_size = data_size;
1292 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1293 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1295 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1298 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1301 temp_sound_data->utt_id = uttid;
1302 temp_sound_data->event = event;
1303 temp_sound_data->audio_type = audio_type;
1304 temp_sound_data->rate = rate;
1306 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1307 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1309 if (NULL != temp_sound_data->data) {
1310 free(temp_sound_data->data);
1311 temp_sound_data->data = NULL;
1314 free(temp_sound_data);
1315 temp_sound_data = NULL;
1318 /* if (0 != ttsd_player_play(uid)) {
1319 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1321 // Change ready state
1322 ttsd_server_stop(uid);
1325 tmp_pid = ttsd_data_get_pid(uid);
1326 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1330 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1333 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1335 return TTSD_ERROR_NONE;