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);
236 if (NULL != temp_sound_data->data) {
237 free(temp_sound_data->data);
238 temp_sound_data->data = NULL;
241 free(temp_sound_data);
242 temp_sound_data = NULL;
244 return TTSD_ERROR_OPERATION_FAILED;
247 if (event == TTSE_RESULT_EVENT_FINISH) {
248 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
251 if (0 != ttsd_player_play(uid)) {
252 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
254 /* Change ready state */
255 ttsd_server_stop(uid);
258 tmp_pid = ttsd_data_get_pid(uid);
259 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
262 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
263 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
267 /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
268 SLOG(LOG_DEBUG, tts_tag(), " ");*/
270 return TTSD_ERROR_NONE;
273 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
275 /* clear client data */
276 ttsd_data_clear_data(uid);
277 ttsd_data_set_client_state(uid, APP_STATE_READY);
280 if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
282 ttsd_data_delete_client(uid);
288 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
291 case TTS_CONFIG_TYPE_ENGINE:
293 /* TODO - Determine the policy when engine process get engine changed cb */
294 if (NULL == str_param) {
295 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
300 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
301 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
302 ret = ttsd_engine_agent_unload_current_engine();
304 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
307 ret = ttsd_engine_agent_load_current_engine();
309 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
314 /* stop all player */
315 ttsd_player_all_stop();
317 /* send interrupt message to all clients */
318 ttsd_data_foreach_clients(__get_client_cb, NULL);
320 ttsd_engine_cancel_synthesis();
325 case TTS_CONFIG_TYPE_VOICE:
327 if (NULL == str_param) {
328 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
332 char* out_lang = NULL;
336 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
337 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
338 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
340 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
342 /* Current language is not available */
343 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
345 if (NULL != out_lang) {
352 case TTS_CONFIG_TYPE_SPEED:
354 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
355 /* set default speed */
357 ret = ttsd_engine_agent_set_default_speed(int_param);
359 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
365 case TTS_CONFIG_TYPE_PITCH:
367 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
368 /* set default speed */
370 ret = ttsd_engine_agent_set_default_pitch(int_param);
372 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
382 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
384 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
385 ttsd_server_finalize(uid);
389 Eina_Bool ttsd_terminate_daemon(void *data)
391 ttsd_data_foreach_clients(__terminate_client, NULL);
395 void __screen_reader_changed_cb(bool value)
397 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
398 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
399 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
401 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
409 int ttsd_initialize(ttse_request_callback_s *callback)
411 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
413 if (ttsd_config_initialize(__config_changed_cb)) {
414 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
418 if (ttsd_player_init()) {
419 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
420 return TTSD_ERROR_OPERATION_FAILED;
423 /* Engine Agent initialize */
424 if (0 != ttsd_engine_agent_init()) {
425 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
426 return TTSD_ERROR_OPERATION_FAILED;
429 /* set current engine */
430 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
431 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
432 // g_is_engine = false;
434 // g_is_engine = true;
436 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
437 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
438 return TTSD_ERROR_OPERATION_FAILED;
441 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
443 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
444 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
447 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
448 if (NULL == g_check_client_timer) {
449 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
452 return TTSD_ERROR_NONE;
457 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
460 if (0 < g_list_length(g_proc_list)) {
461 iter = g_list_first(g_proc_list);
462 while (NULL != iter) {
463 g_proc_list = g_list_remove_link(g_proc_list, iter);
465 iter = g_list_first(g_proc_list);
469 ttsd_config_finalize();
471 ttsd_player_release();
473 ttsd_engine_agent_release();
475 if (NULL != g_check_client_timer) {
476 ecore_timer_del(g_check_client_timer);
477 g_check_client_timer = NULL;
479 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
482 return TTSD_ERROR_NONE;
487 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
489 ttsd_terminate_daemon(NULL);
491 ttsd_dbus_close_connection();
492 ttsd_network_finalize();
495 return TTSD_ERROR_NONE;
499 * TTS Server Functions for Client
502 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
504 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
506 if (-1 != ttsd_data_is_client(uid)) {
507 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
508 return TTSD_ERROR_NONE;
511 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
512 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
513 return TTSD_ERROR_OPERATION_FAILED;
516 if (true == *credential_needed) {
518 if (0 != app_manager_get_app_id(pid, &appid)) {
519 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
521 bool is_agreed = false;
522 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
523 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
526 return TTSD_ERROR_OPERATION_FAILED;
531 if (false == is_agreed) {
532 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
533 return TTSD_ERROR_PERMISSION_DENIED;
537 if (0 != ttsd_data_new_client(pid, uid)) {
538 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
539 return TTSD_ERROR_OPERATION_FAILED;
542 if (0 != ttsd_player_create_instance(uid)) {
543 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
544 return TTSD_ERROR_OPERATION_FAILED;
547 return TTSD_ERROR_NONE;
550 static Eina_Bool __quit_ecore_loop(void *data)
552 ttsd_dbus_close_connection();
553 ttsd_network_finalize();
555 ecore_main_loop_quit();
561 static void __read_proc()
564 struct dirent *dirp = NULL;
568 if (0 < g_list_length(g_proc_list)) {
569 iter = g_list_first(g_proc_list);
570 while (NULL != iter) {
571 g_proc_list = g_list_remove_link(g_proc_list, iter);
573 iter = g_list_first(g_proc_list);
577 dp = opendir("/proc");
579 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
585 tmp = atoi(dirp->d_name);
586 if (0 >= tmp) continue;
587 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
589 } while (NULL != dirp);
595 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
601 for (i = 0; i < g_list_length(g_proc_list); i++) {
602 iter = g_list_nth(g_proc_list, i);
604 if (pid == GPOINTER_TO_INT(iter->data)) {
605 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
612 if (false == exist) {
613 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
614 ttsd_server_finalize(uid);
619 char appid[1024] = {0, };
620 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
621 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
624 if (0 < strlen(appid)) {
625 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
627 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
630 result = ttsdc_send_hello(pid, uid);
633 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
634 ttsd_server_finalize(uid);
635 } else if (-1 == result) {
636 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
644 Eina_Bool ttsd_cleanup_client(void *data)
646 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
649 if (0 < ttsd_data_get_client_count()) {
650 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
652 ecore_timer_add(0, __quit_ecore_loop, NULL);
655 SLOG(LOG_DEBUG, tts_tag(), "@@@");
660 void __used_voice_cb(const char* lang, int type)
662 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
663 if (0 != ttsd_engine_unload_voice(lang, type)) {
664 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
668 int ttsd_server_finalize(int uid)
670 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
672 app_tts_state_e state;
673 if (0 > ttsd_data_get_client_state(uid, &state)) {
674 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
677 ttsd_server_stop(uid);
678 ttsd_player_stop(uid);
680 ttsd_player_destroy_instance(uid);
682 /* Need to unload voice when used voice is unregistered */
683 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
684 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
685 return TTSD_ERROR_OPERATION_FAILED;
688 ttsd_data_delete_client(uid);
690 /* unload engine, if ref count of client is 0 */
691 if (0 == ttsd_data_get_client_count()) {
692 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
693 ecore_timer_add(0, __quit_ecore_loop, NULL);
696 return TTSD_ERROR_NONE;
699 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
701 app_tts_state_e state;
702 if (0 > ttsd_data_get_client_state(uid, &state)) {
703 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
704 return TTSD_ERROR_INVALID_PARAMETER;
707 /* check valid voice */
708 char* temp_lang = NULL;
710 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
711 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
712 if (NULL != temp_lang) {
716 return TTSD_ERROR_INVALID_VOICE;
719 if (NULL == temp_lang) {
720 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
721 return TTSD_ERROR_INVALID_VOICE;
724 speak_data_s* speak_data = NULL;
725 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
726 if (NULL == speak_data) {
727 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
728 if (NULL != temp_lang) {
732 return TTSD_ERROR_OPERATION_FAILED;
735 speak_data->lang = strdup(lang);
736 speak_data->vctype = voice_type;
738 speak_data->speed = speed;
739 speak_data->utt_id = utt_id;
741 speak_data->text = strdup(text);
743 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);
745 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
747 ret = ttsd_data_add_speak_data(uid, speak_data);
749 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
750 if (NULL != temp_lang) {
754 if (NULL != speak_data) {
755 if (NULL != speak_data->lang) free(speak_data->lang);
756 if (NULL != speak_data->text) free(speak_data->text);
758 speak_data->lang = NULL;
759 speak_data->text = NULL;
768 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
769 /* Request load voice */
770 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
771 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
772 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
776 if (NULL != temp_lang) {
781 if (APP_STATE_PLAYING == state) {
782 /* check if engine use network */
783 if (ttsd_engine_agent_need_network()) {
784 if (false == ttsd_network_is_connected()) {
785 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
786 return TTSD_ERROR_OPERATION_FAILED;
790 /* Check whether tts-engine is running or not */
791 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
792 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
794 __synthesis(uid, credential);
798 return TTSD_ERROR_NONE;
801 Eina_Bool __send_interrupt_client(void *data)
803 intptr_t puid = (intptr_t)data;
806 int pid = ttsd_data_get_pid(uid);
808 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
809 /* send message to client about changing state */
810 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
812 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
818 int ttsd_server_play(int uid, const char* credential)
820 app_tts_state_e state;
821 if (0 > ttsd_data_get_client_state(uid, &state)) {
822 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
823 return TTSD_ERROR_INVALID_PARAMETER;
826 if (APP_STATE_PLAYING == state) {
827 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
828 return TTSD_ERROR_NONE;
831 /* check if engine use network */
832 if (ttsd_engine_agent_need_network()) {
833 if (false == ttsd_network_is_connected()) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
835 return TTSD_ERROR_OUT_OF_NETWORK;
839 /* check the current playback focus */
840 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
841 bool is_current_interrupt = false;
842 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
843 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
845 if (true == is_current_interrupt) {
846 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
847 ttsd_data_clear_data(uid);
848 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
853 int current_uid = ttsd_data_get_current_playing();
854 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
856 if (uid != current_uid && -1 != current_uid) {
857 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
858 /* Send interrupt message */
859 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
862 if (0 != ttsd_server_stop(current_uid)) {
863 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
865 if (0 != ttsd_player_stop(current_uid)) {
866 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
869 intptr_t pcurrent_uid = (intptr_t)current_uid;
870 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
872 /* Default mode policy of interrupt is "Pause" */
874 /* Send interrupt message */
875 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
878 if (0 != ttsd_player_pause(current_uid)) {
879 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
883 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
885 intptr_t pcurrent_uid = (intptr_t)current_uid;
886 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
890 /* Change current play */
891 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
892 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
893 return TTSD_ERROR_OPERATION_FAILED;
896 if (APP_STATE_PAUSED == state) {
897 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
900 if (0 != ttsd_player_resume(uid)) {
901 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
905 /* Check whether tts-engine is running or not */
906 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
907 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
909 __synthesis(uid, credential);
912 return TTSD_ERROR_NONE;
915 int ttsd_server_stop(int uid)
917 app_tts_state_e state;
918 if (0 > ttsd_data_get_client_state(uid, &state)) {
919 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
920 return TTSD_ERROR_INVALID_PARAMETER;
923 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
925 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
926 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
927 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
930 ret = ttsd_engine_cancel_synthesis();
932 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
935 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
937 if (0 != ttsd_player_clear(uid))
938 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
940 ttsd_data_set_client_state(uid, APP_STATE_READY);
944 ttsd_data_clear_data(uid);
946 return TTSD_ERROR_NONE;
949 int ttsd_server_pause(int uid, int* utt_id)
951 app_tts_state_e state;
952 if (0 > ttsd_data_get_client_state(uid, &state)) {
953 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
954 return TTSD_ERROR_INVALID_PARAMETER;
957 if (APP_STATE_PLAYING != state) {
958 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
959 return TTSD_ERROR_INVALID_STATE;
962 *utt_id = g_utt.uttid;
963 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
966 ret = ttsd_player_pause(uid);
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
969 return TTSD_ERROR_OPERATION_FAILED;
972 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
974 return TTSD_ERROR_NONE;
977 int ttsd_server_get_support_voices(int uid, GList** voice_list)
979 app_tts_state_e state;
980 if (0 > ttsd_data_get_client_state(uid, &state)) {
981 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
982 return TTSD_ERROR_INVALID_PARAMETER;
986 int ret = ttsd_engine_get_voice_list(voice_list);
988 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
992 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
994 return TTSD_ERROR_NONE;
997 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
999 app_tts_state_e state;
1000 if (0 > ttsd_data_get_client_state(uid, &state)) {
1001 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1002 return TTSD_ERROR_INVALID_PARAMETER;
1005 /* get current voice */
1006 int ret = ttsd_engine_get_default_voice(language, voice_type);
1008 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1012 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1014 return TTSD_ERROR_NONE;
1017 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1019 app_tts_state_e state;
1020 if (0 > ttsd_data_get_client_state(uid, &state)) {
1021 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1022 return TTSD_ERROR_INVALID_PARAMETER;
1025 if (APP_STATE_READY != state) {
1026 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1027 return TTSD_ERROR_INVALID_STATE;
1030 int ret = ttsd_engine_set_private_data(key, data);
1032 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1034 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1040 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1042 app_tts_state_e state;
1043 if (0 > ttsd_data_get_client_state(uid, &state)) {
1044 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1045 return TTSD_ERROR_INVALID_PARAMETER;
1048 if (APP_STATE_READY != state) {
1049 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1050 return TTSD_ERROR_INVALID_STATE;
1053 int ret = ttsd_engine_get_private_data(key, data);
1055 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1057 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1063 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1065 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1068 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1070 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1076 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1078 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1081 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1083 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1089 int ttsd_server_play_pcm(int uid)
1091 app_tts_state_e state;
1092 if (0 > ttsd_data_get_client_state(uid, &state)) {
1093 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1094 return TTSD_ERROR_INVALID_PARAMETER;
1097 if (APP_STATE_PLAYING == state) {
1098 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1099 return TTSD_ERROR_NONE;
1102 int current_uid = ttsd_data_get_current_playing();
1103 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1105 if (uid != current_uid && -1 != current_uid) {
1106 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1107 /* Send interrupt message */
1108 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1111 if (0 != ttsd_server_stop(current_uid)) {
1112 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1114 if (0 != ttsd_player_stop(current_uid)) {
1115 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1118 intptr_t pcurrent_uid = (intptr_t)current_uid;
1119 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1121 /* Default mode policy of interrupt is "Pause" */
1123 /* Send interrupt message */
1124 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1127 if (0 != ttsd_player_pause(current_uid)) {
1128 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1132 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1134 intptr_t pcurrent_uid = (intptr_t)current_uid;
1135 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1139 /* Change current play */
1140 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1142 return TTSD_ERROR_OPERATION_FAILED;
1145 if (APP_STATE_PAUSED == state) {
1146 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1149 if (0 != ttsd_player_resume(uid)) {
1150 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1153 if (0 != ttsd_player_play_pcm(uid)) {
1154 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1156 // Change ready state
1157 ttsd_server_stop_pcm(uid);
1160 tmp_pid = ttsd_data_get_pid(uid);
1161 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1165 return TTSD_ERROR_NONE;
1168 int ttsd_server_stop_pcm(int uid)
1170 app_tts_state_e state;
1171 if (0 > ttsd_data_get_client_state(uid, &state)) {
1172 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1173 return TTSD_ERROR_INVALID_PARAMETER;
1176 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1178 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1179 ttsd_data_set_client_state(uid, APP_STATE_READY);
1182 /* Reset all data */
1183 ttsd_data_clear_data(uid);
1185 ttsd_player_stop(uid);
1188 return TTSD_ERROR_NONE;
1191 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1193 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1197 /* Synthesis is success */
1198 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1199 if (TTSE_RESULT_EVENT_START == event) {
1200 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1201 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1202 uid, uttid, data, data_size, audio_type, rate);
1203 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1204 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1205 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1206 uid, uttid, data, data_size, audio_type, rate);
1208 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1211 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1212 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1213 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1214 return TTSD_ERROR_INVALID_PARAMETER;
1218 sound_data_s* temp_sound_data = NULL;
1219 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1220 if (NULL == temp_sound_data) {
1221 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1222 return TTSD_ERROR_OUT_OF_MEMORY;
1225 temp_sound_data->data = NULL;
1226 temp_sound_data->rate = 0;
1227 temp_sound_data->data_size = 0;
1229 if (0 < data_size) {
1230 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1231 if (NULL != temp_sound_data->data) {
1232 memcpy(temp_sound_data->data, data, data_size);
1233 temp_sound_data->data_size = data_size;
1234 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1235 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1237 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1240 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1243 temp_sound_data->utt_id = uttid;
1244 temp_sound_data->event = event;
1245 temp_sound_data->audio_type = audio_type;
1246 temp_sound_data->rate = rate;
1248 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1249 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1251 if (NULL != temp_sound_data->data) {
1252 free(temp_sound_data->data);
1253 temp_sound_data->data = NULL;
1256 free(temp_sound_data);
1257 temp_sound_data = NULL;
1260 /* if (0 != ttsd_player_play(uid)) {
1261 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1263 // Change ready state
1264 ttsd_server_stop(uid);
1267 tmp_pid = ttsd_data_get_pid(uid);
1268 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1272 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1275 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1277 return TTSD_ERROR_NONE;