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;
42 static utterance_t g_utt;
44 static GList *g_proc_list = NULL;
46 /* Function definitions */
47 static int __synthesis(int uid, const char* credential);
49 static Eina_Bool __wait_synthesis(void *data)
51 /* get current play */
52 char* credential = (char*)data;
53 int uid = ttsd_data_get_current_playing();
56 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
60 if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
61 /* Start next synthesis */
62 __synthesis(uid, credential);
72 static int __synthesis(int uid, const char* credential)
74 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
76 speak_data_s* speak_data = NULL;
77 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
78 if (NULL == speak_data) {
79 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
83 int pid = ttsd_data_get_pid(uid);
84 char appid[1024] = {0, };
85 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
86 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
89 if (NULL == speak_data->lang || NULL == speak_data->text) {
90 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
91 ttsd_server_stop(uid);
93 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
95 ttsd_data_clear_speak_data(uid, &speak_data);
101 g_utt.uttid = speak_data->utt_id;
103 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
104 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
105 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
106 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
107 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
108 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
111 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
112 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
114 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
116 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
118 ttsd_server_stop(uid);
120 int pid = ttsd_data_get_pid(uid);
121 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
123 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
126 ttsd_data_clear_speak_data(uid, &speak_data);
128 ttsd_data_clear_speak_data(uid, &speak_data);
131 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
137 * TTS Server Callback Functions
139 int ttsd_send_error(ttse_error_e error, const char* msg)
142 int uttid = g_utt.uttid;
143 int tmp_pid = ttsd_data_get_pid(uid);
145 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));
147 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
149 if (0 != ttsd_player_clear(uid))
150 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
152 if (0 != ttsd_data_clear_data(uid))
153 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
155 if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
156 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
158 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
159 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
161 if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
162 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
167 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)
169 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
172 int uttid = g_utt.uttid;
174 /* Synthesis is success */
175 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
176 if (TTSE_RESULT_EVENT_START == event) {
177 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
178 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
179 uid, uttid, data, data_size, audio_type, rate);
180 } else if (TTSE_RESULT_EVENT_FINISH == event) {
181 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
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);
185 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
189 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
190 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
191 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
192 SLOG(LOG_DEBUG, tts_tag(), "@@@");
193 return TTSD_ERROR_OPERATION_FAILED;
196 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
197 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
198 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
199 SLOG(LOG_DEBUG, tts_tag(), "@@@");
200 return TTSD_ERROR_INVALID_PARAMETER;
204 sound_data_s* temp_sound_data = NULL;
205 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
206 if (NULL == temp_sound_data) {
207 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
208 return TTSD_ERROR_OUT_OF_MEMORY;
211 temp_sound_data->data = NULL;
212 temp_sound_data->rate = 0;
213 temp_sound_data->data_size = 0;
216 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
217 if (NULL != temp_sound_data->data) {
218 memcpy(temp_sound_data->data, data, data_size);
219 temp_sound_data->data_size = data_size;
220 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
221 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
223 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
226 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
229 temp_sound_data->utt_id = uttid;
230 temp_sound_data->event = event;
231 temp_sound_data->audio_type = audio_type;
232 temp_sound_data->rate = rate;
234 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
235 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
238 if (event == TTSE_RESULT_EVENT_FINISH) {
239 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
242 if (0 != ttsd_player_play(uid)) {
243 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
245 /* Change ready state */
246 ttsd_server_stop(uid);
249 tmp_pid = ttsd_data_get_pid(uid);
250 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
253 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
254 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
258 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
259 SLOG(LOG_DEBUG, tts_tag(), " ");*/
261 return TTSD_ERROR_NONE;
264 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
266 /* clear client data */
267 ttsd_data_clear_data(uid);
268 ttsd_data_set_client_state(uid, APP_STATE_READY);
271 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
273 ttsd_data_delete_client(uid);
279 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
282 case TTS_CONFIG_TYPE_ENGINE:
284 /* TODO - Determine the policy when engine process get engine changed cb */
285 if (NULL == str_param) {
286 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
291 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
292 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
293 ret = ttsd_engine_agent_unload_current_engine();
295 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
298 ret = ttsd_engine_agent_load_current_engine();
300 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
305 /* stop all player */
306 ttsd_player_all_stop();
308 /* send interrupt message to all clients */
309 ttsd_data_foreach_clients(__get_client_cb, NULL);
311 ttsd_engine_cancel_synthesis();
316 case TTS_CONFIG_TYPE_VOICE:
318 if (NULL == str_param) {
319 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
323 char* out_lang = NULL;
327 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
328 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
329 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
331 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
333 /* Current language is not available */
334 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
336 if (NULL != out_lang) {
343 case TTS_CONFIG_TYPE_SPEED:
345 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
346 /* set default speed */
348 ret = ttsd_engine_agent_set_default_speed(int_param);
350 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
356 case TTS_CONFIG_TYPE_PITCH:
358 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
359 /* set default speed */
361 ret = ttsd_engine_agent_set_default_pitch(int_param);
363 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
373 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
375 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
376 ttsd_server_finalize(uid);
380 Eina_Bool ttsd_terminate_daemon(void *data)
382 ttsd_data_foreach_clients(__terminate_client, NULL);
386 void __screen_reader_changed_cb(bool value)
388 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
389 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
390 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
392 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
400 int ttsd_initialize(ttse_request_callback_s *callback)
402 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
404 if (ttsd_config_initialize(__config_changed_cb)) {
405 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
409 if (ttsd_player_init()) {
410 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
411 return TTSD_ERROR_OPERATION_FAILED;
414 /* Engine Agent initialize */
415 if (0 != ttsd_engine_agent_init()) {
416 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
417 return TTSD_ERROR_OPERATION_FAILED;
420 /* set current engine */
421 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
422 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
423 // g_is_engine = false;
425 // g_is_engine = true;
427 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
428 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
429 return TTSD_ERROR_OPERATION_FAILED;
432 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
434 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
435 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
438 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
439 if (NULL == g_check_client_timer) {
440 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
443 return TTSD_ERROR_NONE;
448 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
451 if (0 < g_list_length(g_proc_list)) {
452 iter = g_list_first(g_proc_list);
453 while (NULL != iter) {
454 g_proc_list = g_list_remove_link(g_proc_list, iter);
456 iter = g_list_first(g_proc_list);
460 ttsd_config_finalize();
462 ttsd_player_release();
464 ttsd_engine_agent_release();
466 if (NULL != g_check_client_timer) {
467 ecore_timer_del(g_check_client_timer);
468 g_check_client_timer = NULL;
470 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
473 return TTSD_ERROR_NONE;
478 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
480 ttsd_terminate_daemon(NULL);
482 ttsd_dbus_close_connection();
483 ttsd_network_finalize();
486 return TTSD_ERROR_NONE;
490 * TTS Server Functions for Client
493 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
495 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
497 if (-1 != ttsd_data_is_client(uid)) {
498 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
499 return TTSD_ERROR_NONE;
502 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
503 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
504 return TTSD_ERROR_OPERATION_FAILED;
507 if (true == *credential_needed) {
509 if (0 != app_manager_get_app_id(pid, &appid)) {
510 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
512 bool is_agreed = false;
513 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
514 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
517 return TTSD_ERROR_OPERATION_FAILED;
522 if (false == is_agreed) {
523 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
524 return TTSD_ERROR_PERMISSION_DENIED;
528 if (0 != ttsd_data_new_client(pid, uid)) {
529 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
530 return TTSD_ERROR_OPERATION_FAILED;
533 if (0 != ttsd_player_create_instance(uid)) {
534 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
535 return TTSD_ERROR_OPERATION_FAILED;
538 return TTSD_ERROR_NONE;
541 static Eina_Bool __quit_ecore_loop(void *data)
543 ttsd_dbus_close_connection();
544 ttsd_network_finalize();
546 ecore_main_loop_quit();
552 static void __read_proc()
555 struct dirent *dirp = NULL;
559 if (0 < g_list_length(g_proc_list)) {
560 iter = g_list_first(g_proc_list);
561 while (NULL != iter) {
562 g_proc_list = g_list_remove_link(g_proc_list, iter);
564 iter = g_list_first(g_proc_list);
568 dp = opendir("/proc");
570 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
576 tmp = atoi(dirp->d_name);
577 if (0 >= tmp) continue;
578 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
580 } while (NULL != dirp);
586 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
592 for (i = 0; i < g_list_length(g_proc_list); i++) {
593 iter = g_list_nth(g_proc_list, i);
595 if (pid == GPOINTER_TO_INT(iter->data)) {
596 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
603 if (false == exist) {
604 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
605 ttsd_server_finalize(uid);
610 char appid[1024] = {0, };
611 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
612 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
615 if (0 < strlen(appid)) {
616 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
618 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
621 result = ttsdc_send_hello(pid, uid);
624 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
625 ttsd_server_finalize(uid);
626 } else if (-1 == result) {
627 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
635 Eina_Bool ttsd_cleanup_client(void *data)
637 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
640 if (0 < ttsd_data_get_client_count()) {
641 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
643 ecore_timer_add(0, __quit_ecore_loop, NULL);
646 SLOG(LOG_DEBUG, tts_tag(), "@@@");
651 void __used_voice_cb(const char* lang, int type)
653 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
654 if (0 != ttsd_engine_unload_voice(lang, type)) {
655 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
659 int ttsd_server_finalize(int uid)
661 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
663 app_tts_state_e state;
664 if (0 > ttsd_data_get_client_state(uid, &state)) {
665 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
668 ttsd_server_stop(uid);
669 ttsd_player_stop(uid);
671 ttsd_player_destroy_instance(uid);
673 /* Need to unload voice when used voice is unregistered */
674 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
675 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
676 return TTSD_ERROR_OPERATION_FAILED;
679 ttsd_data_delete_client(uid);
681 /* unload engine, if ref count of client is 0 */
682 if (0 == ttsd_data_get_client_count()) {
683 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
684 ecore_timer_add(0, __quit_ecore_loop, NULL);
687 return TTSD_ERROR_NONE;
690 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
692 app_tts_state_e state;
693 if (0 > ttsd_data_get_client_state(uid, &state)) {
694 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
695 return TTSD_ERROR_INVALID_PARAMETER;
698 /* check valid voice */
699 char* temp_lang = NULL;
701 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
702 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
703 if (NULL != temp_lang) {
707 return TTSD_ERROR_INVALID_VOICE;
710 if (NULL == temp_lang) {
711 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
712 return TTSD_ERROR_INVALID_VOICE;
715 speak_data_s* speak_data = NULL;
716 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
717 if (NULL == speak_data) {
718 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
719 if (NULL != temp_lang) {
723 return TTSD_ERROR_OPERATION_FAILED;
726 speak_data->lang = strdup(lang);
727 speak_data->vctype = voice_type;
729 speak_data->speed = speed;
730 speak_data->utt_id = utt_id;
732 speak_data->text = strdup(text);
734 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);
736 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
738 ret = ttsd_data_add_speak_data(uid, speak_data);
740 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
741 if (NULL != temp_lang) {
745 if (NULL != speak_data) {
746 if (NULL != speak_data->lang) free(speak_data->lang);
747 if (NULL != speak_data->text) free(speak_data->text);
749 speak_data->lang = NULL;
750 speak_data->text = NULL;
759 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
760 /* Request load voice */
761 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
762 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
763 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
767 if (NULL != temp_lang) {
772 if (APP_STATE_PLAYING == state) {
773 /* check if engine use network */
774 if (ttsd_engine_agent_need_network()) {
775 if (false == ttsd_network_is_connected()) {
776 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
777 return TTSD_ERROR_OPERATION_FAILED;
781 /* Check whether tts-engine is running or not */
782 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
783 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
785 __synthesis(uid, credential);
789 return TTSD_ERROR_NONE;
792 Eina_Bool __send_interrupt_client(void *data)
794 intptr_t puid = (intptr_t)data;
797 int pid = ttsd_data_get_pid(uid);
799 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
800 /* send message to client about changing state */
801 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
803 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
809 int ttsd_server_play(int uid, const char* credential)
811 app_tts_state_e state;
812 if (0 > ttsd_data_get_client_state(uid, &state)) {
813 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
814 return TTSD_ERROR_INVALID_PARAMETER;
817 if (APP_STATE_PLAYING == state) {
818 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
819 return TTSD_ERROR_NONE;
822 /* check if engine use network */
823 if (ttsd_engine_agent_need_network()) {
824 if (false == ttsd_network_is_connected()) {
825 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
826 return TTSD_ERROR_OUT_OF_NETWORK;
830 /* check the current playback focus */
831 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
832 bool is_current_interrupt = false;
833 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
836 if (true == is_current_interrupt) {
837 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
838 ttsd_data_clear_data(uid);
839 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
844 int current_uid = ttsd_data_get_current_playing();
845 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
847 if (uid != current_uid && -1 != current_uid) {
848 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
849 /* Send interrupt message */
850 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
853 if (0 != ttsd_server_stop(current_uid)) {
854 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
856 if (0 != ttsd_player_stop(current_uid)) {
857 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
860 intptr_t pcurrent_uid = (intptr_t)current_uid;
861 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
863 /* Default mode policy of interrupt is "Pause" */
865 /* Send interrupt message */
866 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
869 if (0 != ttsd_player_pause(current_uid)) {
870 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
874 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
876 intptr_t pcurrent_uid = (intptr_t)current_uid;
877 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
881 /* Change current play */
882 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
883 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
884 return TTSD_ERROR_OPERATION_FAILED;
887 if (APP_STATE_PAUSED == state) {
888 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
891 if (0 != ttsd_player_resume(uid)) {
892 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
896 /* Check whether tts-engine is running or not */
897 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
898 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
900 __synthesis(uid, credential);
903 return TTSD_ERROR_NONE;
906 int ttsd_server_stop(int uid)
908 app_tts_state_e state;
909 if (0 > ttsd_data_get_client_state(uid, &state)) {
910 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
911 return TTSD_ERROR_INVALID_PARAMETER;
914 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
916 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
917 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
918 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
921 ret = ttsd_engine_cancel_synthesis();
923 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
926 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
928 if (0 != ttsd_player_clear(uid))
929 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
931 ttsd_data_set_client_state(uid, APP_STATE_READY);
935 ttsd_data_clear_data(uid);
937 return TTSD_ERROR_NONE;
940 int ttsd_server_pause(int uid, int* utt_id)
942 app_tts_state_e state;
943 if (0 > ttsd_data_get_client_state(uid, &state)) {
944 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
945 return TTSD_ERROR_INVALID_PARAMETER;
948 if (APP_STATE_PLAYING != state) {
949 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
950 return TTSD_ERROR_INVALID_STATE;
953 *utt_id = g_utt.uttid;
954 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
957 ret = ttsd_player_pause(uid);
959 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
960 return TTSD_ERROR_OPERATION_FAILED;
963 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
965 return TTSD_ERROR_NONE;
968 int ttsd_server_get_support_voices(int uid, GList** voice_list)
970 app_tts_state_e state;
971 if (0 > ttsd_data_get_client_state(uid, &state)) {
972 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
973 return TTSD_ERROR_INVALID_PARAMETER;
977 int ret = ttsd_engine_get_voice_list(voice_list);
979 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
983 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
985 return TTSD_ERROR_NONE;
988 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
990 app_tts_state_e state;
991 if (0 > ttsd_data_get_client_state(uid, &state)) {
992 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
993 return TTSD_ERROR_INVALID_PARAMETER;
996 /* get current voice */
997 int ret = ttsd_engine_get_default_voice(language, voice_type);
999 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1003 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1005 return TTSD_ERROR_NONE;
1008 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1010 app_tts_state_e state;
1011 if (0 > ttsd_data_get_client_state(uid, &state)) {
1012 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1013 return TTSD_ERROR_INVALID_PARAMETER;
1016 if (APP_STATE_READY != state) {
1017 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1018 return TTSD_ERROR_INVALID_STATE;
1021 int ret = ttsd_engine_set_private_data(key, data);
1023 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1025 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1031 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1033 app_tts_state_e state;
1034 if (0 > ttsd_data_get_client_state(uid, &state)) {
1035 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1036 return TTSD_ERROR_INVALID_PARAMETER;
1039 if (APP_STATE_READY != state) {
1040 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1041 return TTSD_ERROR_INVALID_STATE;
1044 int ret = ttsd_engine_get_private_data(key, data);
1046 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1048 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1054 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1056 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1059 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1061 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1067 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1069 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1072 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1074 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1080 int ttsd_server_play_pcm(int uid)
1082 app_tts_state_e state;
1083 if (0 > ttsd_data_get_client_state(uid, &state)) {
1084 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1085 return TTSD_ERROR_INVALID_PARAMETER;
1088 if (APP_STATE_PLAYING == state) {
1089 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1090 return TTSD_ERROR_NONE;
1093 int current_uid = ttsd_data_get_current_playing();
1094 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1096 if (uid != current_uid && -1 != current_uid) {
1097 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1098 /* Send interrupt message */
1099 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1102 if (0 != ttsd_server_stop(current_uid)) {
1103 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1105 if (0 != ttsd_player_stop(current_uid)) {
1106 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1109 intptr_t pcurrent_uid = (intptr_t)current_uid;
1110 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1112 /* Default mode policy of interrupt is "Pause" */
1114 /* Send interrupt message */
1115 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1118 if (0 != ttsd_player_pause(current_uid)) {
1119 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1123 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1125 intptr_t pcurrent_uid = (intptr_t)current_uid;
1126 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1130 /* Change current play */
1131 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1132 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1133 return TTSD_ERROR_OPERATION_FAILED;
1136 if (APP_STATE_PAUSED == state) {
1137 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1140 if (0 != ttsd_player_resume(uid)) {
1141 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1144 if (0 != ttsd_player_play_pcm(uid)) {
1145 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1147 // Change ready state
1148 ttsd_server_stop_pcm(uid);
1151 tmp_pid = ttsd_data_get_pid(uid);
1152 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1156 return TTSD_ERROR_NONE;
1159 int ttsd_server_stop_pcm(int uid)
1161 app_tts_state_e state;
1162 if (0 > ttsd_data_get_client_state(uid, &state)) {
1163 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1164 return TTSD_ERROR_INVALID_PARAMETER;
1167 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1169 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1170 ttsd_data_set_client_state(uid, APP_STATE_READY);
1173 /* Reset all data */
1174 ttsd_data_clear_data(uid);
1176 ttsd_player_stop(uid);
1179 return TTSD_ERROR_NONE;
1182 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1184 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1188 /* Synthesis is success */
1189 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1190 if (TTSE_RESULT_EVENT_START == event) {
1191 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1192 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1193 uid, uttid, data, data_size, audio_type, rate);
1194 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1195 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1196 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1197 uid, uttid, data, data_size, audio_type, rate);
1199 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1202 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1203 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1204 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1205 return TTSD_ERROR_INVALID_PARAMETER;
1209 sound_data_s* temp_sound_data = NULL;
1210 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1211 if (NULL == temp_sound_data) {
1212 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1213 return TTSD_ERROR_OUT_OF_MEMORY;
1216 temp_sound_data->data = NULL;
1217 temp_sound_data->rate = 0;
1218 temp_sound_data->data_size = 0;
1220 if (0 < data_size) {
1221 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1222 if (NULL != temp_sound_data->data) {
1223 memcpy(temp_sound_data->data, data, data_size);
1224 temp_sound_data->data_size = data_size;
1225 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1226 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1228 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1231 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1234 temp_sound_data->utt_id = uttid;
1235 temp_sound_data->event = event;
1236 temp_sound_data->audio_type = audio_type;
1237 temp_sound_data->rate = rate;
1239 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1240 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1242 if (NULL != temp_sound_data->data) {
1243 free(temp_sound_data->data);
1244 temp_sound_data->data = NULL;
1247 free(temp_sound_data);
1248 temp_sound_data = NULL;
1251 /* if (0 != ttsd_player_play(uid)) {
1252 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1254 // Change ready state
1255 ttsd_server_stop(uid);
1258 tmp_pid = ttsd_data_get_pid(uid);
1259 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1263 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1266 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1268 return TTSD_ERROR_NONE;