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[128] = {0, };
106 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
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 if (NULL != speak_data) {
117 if (NULL != speak_data->lang) free(speak_data->lang);
118 if (NULL != speak_data->text) free(speak_data->text);
120 speak_data->lang = NULL;
121 speak_data->text = NULL;
131 g_utt.uttid = speak_data->utt_id;
133 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
134 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
135 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
136 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
137 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
138 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
141 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
142 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
144 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
146 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
148 ttsd_server_stop(uid);
150 int pid = ttsd_data_get_pid(uid);
151 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
153 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
156 if (NULL != speak_data) {
157 if (NULL != speak_data->lang) free(speak_data->lang);
158 if (NULL != speak_data->text) free(speak_data->text);
160 speak_data->lang = NULL;
161 speak_data->text = NULL;
168 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
174 * TTS Server Callback Functions
176 int ttsd_send_error(ttse_error_e error, const char* msg)
179 int uttid = g_utt.uttid;
180 int tmp_pid = ttsd_data_get_pid(uid);
182 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
184 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
186 if (0 != ttsd_player_clear(uid))
187 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
189 if (0 != ttsd_data_clear_data(uid))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
192 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
193 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
195 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
196 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
198 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
199 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
204 int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
206 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
209 int uttid = g_utt.uttid;
211 /* Synthesis is success */
212 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
213 if (TTSE_RESULT_EVENT_START == event) {
214 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
215 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
216 uid, uttid, data, data_size, audio_type, rate);
217 } else if (TTSE_RESULT_EVENT_FINISH == event) {
218 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
219 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
220 uid, uttid, data, data_size, audio_type, rate);
222 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
226 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
227 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
228 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
229 SLOG(LOG_DEBUG, tts_tag(), "@@@");
230 return TTSD_ERROR_OPERATION_FAILED;
233 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
234 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
235 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
236 SLOG(LOG_DEBUG, tts_tag(), "@@@");
237 return TTSD_ERROR_INVALID_PARAMETER;
241 sound_data_s* temp_sound_data = NULL;
242 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
243 if (NULL == temp_sound_data) {
244 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
245 return TTSD_ERROR_OUT_OF_MEMORY;
248 temp_sound_data->data = NULL;
249 temp_sound_data->rate = 0;
250 temp_sound_data->data_size = 0;
253 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
254 if (NULL != temp_sound_data->data) {
255 memcpy(temp_sound_data->data, data, data_size);
256 temp_sound_data->data_size = data_size;
257 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
258 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
260 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
263 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
266 temp_sound_data->utt_id = uttid;
267 temp_sound_data->event = event;
268 temp_sound_data->audio_type = audio_type;
269 temp_sound_data->rate = rate;
271 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
272 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
275 if (event == TTSE_RESULT_EVENT_FINISH) {
276 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
279 if (0 != ttsd_player_play(uid)) {
280 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
282 /* Change ready state */
283 ttsd_server_stop(uid);
286 tmp_pid = ttsd_data_get_pid(uid);
287 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
290 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
291 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
295 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
296 SLOG(LOG_DEBUG, tts_tag(), " ");*/
298 return TTSD_ERROR_NONE;
301 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
303 /* clear client data */
304 ttsd_data_clear_data(uid);
305 ttsd_data_set_client_state(uid, APP_STATE_READY);
308 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
310 ttsd_data_delete_client(uid);
316 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
319 case TTS_CONFIG_TYPE_ENGINE:
321 /* TODO - Determine the policy when engine process get engine changed cb */
322 if (NULL == str_param) {
323 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
328 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
329 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
330 ret = ttsd_engine_agent_unload_current_engine();
332 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
335 ret = ttsd_engine_agent_load_current_engine();
337 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
342 /* stop all player */
343 ttsd_player_all_stop();
345 /* send interrupt message to all clients */
346 ttsd_data_foreach_clients(__get_client_cb, NULL);
348 ttsd_engine_cancel_synthesis();
353 case TTS_CONFIG_TYPE_VOICE:
355 if (NULL == str_param) {
356 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
360 char* out_lang = NULL;
364 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
365 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
366 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
368 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
370 /* Current language is not available */
371 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
373 if (NULL != out_lang) {
380 case TTS_CONFIG_TYPE_SPEED:
382 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
383 /* set default speed */
385 ret = ttsd_engine_agent_set_default_speed(int_param);
387 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
393 case TTS_CONFIG_TYPE_PITCH:
395 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
396 /* set default speed */
398 ret = ttsd_engine_agent_set_default_pitch(int_param);
400 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
410 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
412 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
413 ttsd_server_finalize(uid);
417 Eina_Bool ttsd_terminate_daemon(void *data)
419 ttsd_data_foreach_clients(__terminate_client, NULL);
423 void __screen_reader_changed_cb(bool value)
425 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
426 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
427 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 (ttsd_player_init()) {
447 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
448 return TTSD_ERROR_OPERATION_FAILED;
451 /* Engine Agent initialize */
452 if (0 != ttsd_engine_agent_init()) {
453 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
454 return TTSD_ERROR_OPERATION_FAILED;
457 /* set current engine */
458 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
459 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
460 // g_is_engine = false;
462 // g_is_engine = true;
464 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
465 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
466 return TTSD_ERROR_OPERATION_FAILED;
469 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
471 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
472 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
475 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
476 if (NULL == g_check_client_timer) {
477 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
480 return TTSD_ERROR_NONE;
485 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
488 if (0 < g_list_length(g_proc_list)) {
489 iter = g_list_first(g_proc_list);
490 while (NULL != iter) {
491 g_proc_list = g_list_remove_link(g_proc_list, iter);
493 iter = g_list_first(g_proc_list);
497 ttsd_config_finalize();
499 ttsd_player_release();
501 ttsd_engine_agent_release();
503 if (NULL != g_check_client_timer) {
504 ecore_timer_del(g_check_client_timer);
505 g_check_client_timer = NULL;
507 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
510 return TTSD_ERROR_NONE;
514 * TTS Server Functions for Client
517 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
519 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
521 if (-1 != ttsd_data_is_client(uid)) {
522 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
523 return TTSD_ERROR_NONE;
526 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
527 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
528 return TTSD_ERROR_OPERATION_FAILED;
531 if (true == *credential_needed) {
533 if (0 != app_manager_get_app_id(pid, &appid)) {
534 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
536 bool is_agreed = false;
537 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
538 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
541 return TTSD_ERROR_OPERATION_FAILED;
546 if (false == is_agreed) {
547 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
548 return TTSD_ERROR_PERMISSION_DENIED;
552 if (0 != ttsd_data_new_client(pid, uid)) {
553 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
554 return TTSD_ERROR_OPERATION_FAILED;
557 if (0 != ttsd_player_create_instance(uid)) {
558 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
559 return TTSD_ERROR_OPERATION_FAILED;
562 return TTSD_ERROR_NONE;
565 static Eina_Bool __quit_ecore_loop(void *data)
567 ttsd_dbus_close_connection();
568 ttsd_network_finalize();
570 ecore_main_loop_quit();
576 static void __read_proc()
579 struct dirent *dirp = NULL;
583 if (0 < g_list_length(g_proc_list)) {
584 iter = g_list_first(g_proc_list);
585 while (NULL != iter) {
586 g_proc_list = g_list_remove_link(g_proc_list, iter);
588 iter = g_list_first(g_proc_list);
592 dp = opendir("/proc");
594 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
600 tmp = atoi(dirp->d_name);
601 if (0 >= tmp) continue;
602 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
604 } while (NULL != dirp);
610 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
616 for (i = 0; i < g_list_length(g_proc_list); i++) {
617 iter = g_list_nth(g_proc_list, i);
619 if (pid == GPOINTER_TO_INT(iter->data)) {
620 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
627 if (false == exist) {
628 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
629 ttsd_server_finalize(uid);
634 char appid[128] = {0, };
635 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
636 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
639 if (0 < strlen(appid)) {
640 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
642 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
645 result = ttsdc_send_hello(pid, uid);
648 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
649 ttsd_server_finalize(uid);
650 } else if (-1 == result) {
651 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
659 Eina_Bool ttsd_cleanup_client(void *data)
661 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
664 if (0 < ttsd_data_get_client_count()) {
665 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
667 ecore_timer_add(0, __quit_ecore_loop, NULL);
670 SLOG(LOG_DEBUG, tts_tag(), "@@@");
675 void __used_voice_cb(const char* lang, int type)
677 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
678 if (0 != ttsd_engine_unload_voice(lang, type)) {
679 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
683 int ttsd_server_finalize(int uid)
685 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
687 app_tts_state_e state;
688 if (0 > ttsd_data_get_client_state(uid, &state)) {
689 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
692 ttsd_server_stop(uid);
693 ttsd_player_stop(uid);
695 ttsd_player_destroy_instance(uid);
697 /* Need to unload voice when used voice is unregistered */
698 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
699 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
700 return TTSD_ERROR_OPERATION_FAILED;
703 ttsd_data_delete_client(uid);
705 /* unload engine, if ref count of client is 0 */
706 if (0 == ttsd_data_get_client_count()) {
707 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
708 ecore_timer_add(0, __quit_ecore_loop, NULL);
711 return TTSD_ERROR_NONE;
714 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
716 app_tts_state_e state;
717 if (0 > ttsd_data_get_client_state(uid, &state)) {
718 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
719 return TTSD_ERROR_INVALID_PARAMETER;
722 /* check valid voice */
723 char* temp_lang = NULL;
725 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
726 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
727 if (NULL != temp_lang) {
731 return TTSD_ERROR_INVALID_VOICE;
734 if (NULL == temp_lang) {
735 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
736 return TTSD_ERROR_INVALID_VOICE;
739 speak_data_s* speak_data = NULL;
740 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
741 if (NULL == speak_data) {
742 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
743 if (NULL != temp_lang) {
747 return TTSD_ERROR_OPERATION_FAILED;
750 speak_data->lang = strdup(lang);
751 speak_data->vctype = voice_type;
753 speak_data->speed = speed;
754 speak_data->utt_id = utt_id;
756 speak_data->text = strdup(text);
758 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);
760 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
762 ret = ttsd_data_add_speak_data(uid, speak_data);
764 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
765 if (NULL != temp_lang) {
769 if (NULL != speak_data) {
770 if (NULL != speak_data->lang) free(speak_data->lang);
771 if (NULL != speak_data->text) free(speak_data->text);
773 speak_data->lang = NULL;
774 speak_data->text = NULL;
783 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
784 /* Request load voice */
785 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
786 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
787 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
791 if (NULL != temp_lang) {
796 if (APP_STATE_PLAYING == state) {
797 /* check if engine use network */
798 if (ttsd_engine_agent_need_network()) {
799 if (false == ttsd_network_is_connected()) {
800 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
801 return TTSD_ERROR_OPERATION_FAILED;
805 /* Check whether tts-engine is running or not */
806 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
807 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
809 __synthesis(uid, credential);
813 return TTSD_ERROR_NONE;
816 Eina_Bool __send_interrupt_client(void *data)
818 intptr_t puid = (intptr_t)data;
821 int pid = ttsd_data_get_pid(uid);
823 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
824 /* send message to client about changing state */
825 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
827 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
833 int ttsd_server_play(int uid, const char* credential)
835 app_tts_state_e state;
836 if (0 > ttsd_data_get_client_state(uid, &state)) {
837 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
838 return TTSD_ERROR_INVALID_PARAMETER;
841 if (APP_STATE_PLAYING == state) {
842 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
843 return TTSD_ERROR_NONE;
846 /* check if engine use network */
847 if (ttsd_engine_agent_need_network()) {
848 if (false == ttsd_network_is_connected()) {
849 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
850 return TTSD_ERROR_OUT_OF_NETWORK;
854 int current_uid = ttsd_data_get_current_playing();
855 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
857 if (uid != current_uid && -1 != current_uid) {
858 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
859 /* Send interrupt message */
860 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
863 if (0 != ttsd_server_stop(current_uid)) {
864 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
866 if (0 != ttsd_player_stop(current_uid)) {
867 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
870 intptr_t pcurrent_uid = (intptr_t)current_uid;
871 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
873 /* Default mode policy of interrupt is "Pause" */
875 /* Send interrupt message */
876 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
879 if (0 != ttsd_player_pause(current_uid)) {
880 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
884 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
886 intptr_t pcurrent_uid = (intptr_t)current_uid;
887 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
891 /* Change current play */
892 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
893 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
894 return TTSD_ERROR_OPERATION_FAILED;
897 if (APP_STATE_PAUSED == state) {
898 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
901 if (0 != ttsd_player_resume(uid)) {
902 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
906 /* Check whether tts-engine is running or not */
907 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
908 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
910 __synthesis(uid, credential);
913 return TTSD_ERROR_NONE;
916 int ttsd_server_stop(int uid)
918 app_tts_state_e state;
919 if (0 > ttsd_data_get_client_state(uid, &state)) {
920 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
921 return TTSD_ERROR_INVALID_PARAMETER;
924 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
926 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
927 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
928 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
931 ret = ttsd_engine_cancel_synthesis();
933 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
936 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
938 if (0 != ttsd_player_clear(uid))
939 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
941 ttsd_data_set_client_state(uid, APP_STATE_READY);
945 ttsd_data_clear_data(uid);
947 return TTSD_ERROR_NONE;
950 int ttsd_server_pause(int uid, int* utt_id)
952 app_tts_state_e state;
953 if (0 > ttsd_data_get_client_state(uid, &state)) {
954 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
955 return TTSD_ERROR_INVALID_PARAMETER;
958 if (APP_STATE_PLAYING != state) {
959 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
960 return TTSD_ERROR_INVALID_STATE;
963 *utt_id = g_utt.uttid;
964 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
967 ret = ttsd_player_pause(uid);
969 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
970 return TTSD_ERROR_OPERATION_FAILED;
973 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
975 return TTSD_ERROR_NONE;
978 int ttsd_server_get_support_voices(int uid, GList** voice_list)
980 app_tts_state_e state;
981 if (0 > ttsd_data_get_client_state(uid, &state)) {
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
983 return TTSD_ERROR_INVALID_PARAMETER;
987 int ret = ttsd_engine_get_voice_list(voice_list);
989 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
993 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
995 return TTSD_ERROR_NONE;
998 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1000 app_tts_state_e state;
1001 if (0 > ttsd_data_get_client_state(uid, &state)) {
1002 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1003 return TTSD_ERROR_INVALID_PARAMETER;
1006 /* get current voice */
1007 int ret = ttsd_engine_get_default_voice(language, voice_type);
1009 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1013 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1015 return TTSD_ERROR_NONE;
1018 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1020 app_tts_state_e state;
1021 if (0 > ttsd_data_get_client_state(uid, &state)) {
1022 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1023 return TTSD_ERROR_INVALID_PARAMETER;
1026 if (APP_STATE_READY != state) {
1027 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1028 return TTSD_ERROR_INVALID_STATE;
1031 int ret = ttsd_engine_set_private_data(key, data);
1033 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1035 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1041 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1043 app_tts_state_e state;
1044 if (0 > ttsd_data_get_client_state(uid, &state)) {
1045 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1046 return TTSD_ERROR_INVALID_PARAMETER;
1049 if (APP_STATE_READY != state) {
1050 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1051 return TTSD_ERROR_INVALID_STATE;
1054 int ret = ttsd_engine_get_private_data(key, data);
1056 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1058 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1064 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1066 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1069 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1071 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1077 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1079 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1082 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1084 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1090 int ttsd_server_play_pcm(int uid)
1092 app_tts_state_e state;
1093 if (0 > ttsd_data_get_client_state(uid, &state)) {
1094 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1095 return TTSD_ERROR_INVALID_PARAMETER;
1098 if (APP_STATE_PLAYING == state) {
1099 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1100 return TTSD_ERROR_NONE;
1103 int current_uid = ttsd_data_get_current_playing();
1104 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1106 if (uid != current_uid && -1 != current_uid) {
1107 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1108 /* Send interrupt message */
1109 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1112 if (0 != ttsd_server_stop(current_uid)) {
1113 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1115 if (0 != ttsd_player_stop(current_uid)) {
1116 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1119 intptr_t pcurrent_uid = (intptr_t)current_uid;
1120 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1122 /* Default mode policy of interrupt is "Pause" */
1124 /* Send interrupt message */
1125 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1128 if (0 != ttsd_player_pause(current_uid)) {
1129 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1133 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1135 intptr_t pcurrent_uid = (intptr_t)current_uid;
1136 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1140 /* Change current play */
1141 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1142 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1143 return TTSD_ERROR_OPERATION_FAILED;
1146 return TTSD_ERROR_NONE;
1149 int ttsd_server_stop_pcm(int uid)
1151 app_tts_state_e state;
1152 if (0 > ttsd_data_get_client_state(uid, &state)) {
1153 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1154 return TTSD_ERROR_INVALID_PARAMETER;
1157 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1159 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1160 if (0 != ttsd_player_clear(uid))
1161 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1163 ttsd_data_set_client_state(uid, APP_STATE_READY);
1166 /* Reset all data */
1167 ttsd_data_clear_data(uid);
1169 return TTSD_ERROR_NONE;
1172 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1174 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1178 /* Synthesis is success */
1179 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1180 if (TTSE_RESULT_EVENT_START == event) {
1181 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1182 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1183 uid, uttid, data, data_size, audio_type, rate);
1184 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1185 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1186 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1187 uid, uttid, data, data_size, audio_type, rate);
1189 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1192 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1193 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1194 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1195 return TTSD_ERROR_INVALID_PARAMETER;
1199 sound_data_s* temp_sound_data = NULL;
1200 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1201 if (NULL == temp_sound_data) {
1202 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1203 return TTSD_ERROR_OUT_OF_MEMORY;
1206 temp_sound_data->data = NULL;
1207 temp_sound_data->rate = 0;
1208 temp_sound_data->data_size = 0;
1210 if (0 < data_size) {
1211 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1212 if (NULL != temp_sound_data->data) {
1213 memcpy(temp_sound_data->data, data, data_size);
1214 temp_sound_data->data_size = data_size;
1215 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1216 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1218 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1221 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1224 temp_sound_data->utt_id = uttid;
1225 temp_sound_data->event = event;
1226 temp_sound_data->audio_type = audio_type;
1227 temp_sound_data->rate = rate;
1229 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1230 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1232 if (NULL != temp_sound_data->data) {
1233 free(temp_sound_data->data);
1234 temp_sound_data->data = NULL;
1237 free(temp_sound_data);
1238 temp_sound_data = NULL;
1241 if (0 != ttsd_player_play(uid)) {
1242 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1244 /* Change ready state */
1245 ttsd_server_stop(uid);
1248 tmp_pid = ttsd_data_get_pid(uid);
1249 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1252 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1255 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1257 return TTSD_ERROR_NONE;