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(&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(&speak_data);
128 ttsd_data_clear_speak_data(&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;
477 * TTS Server Functions for Client
480 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
482 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
484 if (-1 != ttsd_data_is_client(uid)) {
485 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
486 return TTSD_ERROR_NONE;
489 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
490 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
491 return TTSD_ERROR_OPERATION_FAILED;
494 if (true == *credential_needed) {
496 if (0 != app_manager_get_app_id(pid, &appid)) {
497 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
499 bool is_agreed = false;
500 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
501 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
504 return TTSD_ERROR_OPERATION_FAILED;
509 if (false == is_agreed) {
510 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
511 return TTSD_ERROR_PERMISSION_DENIED;
515 if (0 != ttsd_data_new_client(pid, uid)) {
516 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
517 return TTSD_ERROR_OPERATION_FAILED;
520 if (0 != ttsd_player_create_instance(uid)) {
521 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
522 return TTSD_ERROR_OPERATION_FAILED;
525 return TTSD_ERROR_NONE;
528 static Eina_Bool __quit_ecore_loop(void *data)
530 ttsd_dbus_close_connection();
531 ttsd_network_finalize();
533 ecore_main_loop_quit();
539 static void __read_proc()
542 struct dirent *dirp = NULL;
546 if (0 < g_list_length(g_proc_list)) {
547 iter = g_list_first(g_proc_list);
548 while (NULL != iter) {
549 g_proc_list = g_list_remove_link(g_proc_list, iter);
551 iter = g_list_first(g_proc_list);
555 dp = opendir("/proc");
557 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
563 tmp = atoi(dirp->d_name);
564 if (0 >= tmp) continue;
565 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
567 } while (NULL != dirp);
573 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
579 for (i = 0; i < g_list_length(g_proc_list); i++) {
580 iter = g_list_nth(g_proc_list, i);
582 if (pid == GPOINTER_TO_INT(iter->data)) {
583 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
590 if (false == exist) {
591 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
592 ttsd_server_finalize(uid);
597 char appid[1024] = {0, };
598 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
599 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
602 if (0 < strlen(appid)) {
603 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
605 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
608 result = ttsdc_send_hello(pid, uid);
611 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
612 ttsd_server_finalize(uid);
613 } else if (-1 == result) {
614 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
622 Eina_Bool ttsd_cleanup_client(void *data)
624 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
627 if (0 < ttsd_data_get_client_count()) {
628 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
630 ecore_timer_add(0, __quit_ecore_loop, NULL);
633 SLOG(LOG_DEBUG, tts_tag(), "@@@");
638 void __used_voice_cb(const char* lang, int type)
640 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
641 if (0 != ttsd_engine_unload_voice(lang, type)) {
642 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
646 int ttsd_server_finalize(int uid)
648 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
650 app_tts_state_e state;
651 if (0 > ttsd_data_get_client_state(uid, &state)) {
652 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
655 ttsd_server_stop(uid);
656 ttsd_player_stop(uid);
658 ttsd_player_destroy_instance(uid);
660 /* Need to unload voice when used voice is unregistered */
661 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
662 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
663 return TTSD_ERROR_OPERATION_FAILED;
666 ttsd_data_delete_client(uid);
668 /* unload engine, if ref count of client is 0 */
669 if (0 == ttsd_data_get_client_count()) {
670 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
671 ecore_timer_add(0, __quit_ecore_loop, NULL);
674 return TTSD_ERROR_NONE;
677 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
679 app_tts_state_e state;
680 if (0 > ttsd_data_get_client_state(uid, &state)) {
681 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
682 return TTSD_ERROR_INVALID_PARAMETER;
685 /* check valid voice */
686 char* temp_lang = NULL;
688 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
689 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
690 if (NULL != temp_lang) {
694 return TTSD_ERROR_INVALID_VOICE;
697 if (NULL == temp_lang) {
698 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
699 return TTSD_ERROR_INVALID_VOICE;
702 speak_data_s* speak_data = NULL;
703 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
704 if (NULL == speak_data) {
705 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
706 if (NULL != temp_lang) {
710 return TTSD_ERROR_OPERATION_FAILED;
713 speak_data->lang = strdup(lang);
714 speak_data->vctype = voice_type;
716 speak_data->speed = speed;
717 speak_data->utt_id = utt_id;
719 speak_data->text = strdup(text);
721 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);
723 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
725 ret = ttsd_data_add_speak_data(uid, speak_data);
727 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
728 if (NULL != temp_lang) {
732 if (NULL != speak_data) {
733 if (NULL != speak_data->lang) free(speak_data->lang);
734 if (NULL != speak_data->text) free(speak_data->text);
736 speak_data->lang = NULL;
737 speak_data->text = NULL;
746 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
747 /* Request load voice */
748 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
749 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
750 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
754 if (NULL != temp_lang) {
759 if (APP_STATE_PLAYING == state) {
760 /* check if engine use network */
761 if (ttsd_engine_agent_need_network()) {
762 if (false == ttsd_network_is_connected()) {
763 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
764 return TTSD_ERROR_OPERATION_FAILED;
768 /* Check whether tts-engine is running or not */
769 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
770 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
772 __synthesis(uid, credential);
776 return TTSD_ERROR_NONE;
779 Eina_Bool __send_interrupt_client(void *data)
781 intptr_t puid = (intptr_t)data;
784 int pid = ttsd_data_get_pid(uid);
786 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
787 /* send message to client about changing state */
788 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
790 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
796 int ttsd_server_play(int uid, const char* credential)
798 app_tts_state_e state;
799 if (0 > ttsd_data_get_client_state(uid, &state)) {
800 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
801 return TTSD_ERROR_INVALID_PARAMETER;
804 if (APP_STATE_PLAYING == state) {
805 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
806 return TTSD_ERROR_NONE;
809 /* check if engine use network */
810 if (ttsd_engine_agent_need_network()) {
811 if (false == ttsd_network_is_connected()) {
812 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
813 return TTSD_ERROR_OUT_OF_NETWORK;
817 /* check the current playback focus */
818 if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
819 bool is_current_interrupt = false;
820 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
821 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
823 if (true == is_current_interrupt) {
824 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
825 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
830 int current_uid = ttsd_data_get_current_playing();
831 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
833 if (uid != current_uid && -1 != current_uid) {
834 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
835 /* Send interrupt message */
836 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
839 if (0 != ttsd_server_stop(current_uid)) {
840 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
842 if (0 != ttsd_player_stop(current_uid)) {
843 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
846 intptr_t pcurrent_uid = (intptr_t)current_uid;
847 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
849 /* Default mode policy of interrupt is "Pause" */
851 /* Send interrupt message */
852 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
855 if (0 != ttsd_player_pause(current_uid)) {
856 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
860 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
862 intptr_t pcurrent_uid = (intptr_t)current_uid;
863 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
867 /* Change current play */
868 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
869 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
870 return TTSD_ERROR_OPERATION_FAILED;
873 if (APP_STATE_PAUSED == state) {
874 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
877 if (0 != ttsd_player_resume(uid)) {
878 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
882 /* Check whether tts-engine is running or not */
883 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
884 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
886 __synthesis(uid, credential);
889 return TTSD_ERROR_NONE;
892 int ttsd_server_stop(int uid)
894 app_tts_state_e state;
895 if (0 > ttsd_data_get_client_state(uid, &state)) {
896 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
897 return TTSD_ERROR_INVALID_PARAMETER;
900 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
902 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
903 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
904 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
907 ret = ttsd_engine_cancel_synthesis();
909 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
912 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
914 if (0 != ttsd_player_clear(uid))
915 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
917 ttsd_data_set_client_state(uid, APP_STATE_READY);
921 ttsd_data_clear_data(uid);
923 return TTSD_ERROR_NONE;
926 int ttsd_server_pause(int uid, int* utt_id)
928 app_tts_state_e state;
929 if (0 > ttsd_data_get_client_state(uid, &state)) {
930 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
931 return TTSD_ERROR_INVALID_PARAMETER;
934 if (APP_STATE_PLAYING != state) {
935 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
936 return TTSD_ERROR_INVALID_STATE;
939 *utt_id = g_utt.uttid;
940 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
943 ret = ttsd_player_pause(uid);
945 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
946 return TTSD_ERROR_OPERATION_FAILED;
949 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
951 return TTSD_ERROR_NONE;
954 int ttsd_server_get_support_voices(int uid, GList** voice_list)
956 app_tts_state_e state;
957 if (0 > ttsd_data_get_client_state(uid, &state)) {
958 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
959 return TTSD_ERROR_INVALID_PARAMETER;
963 int ret = ttsd_engine_get_voice_list(voice_list);
965 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
969 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
971 return TTSD_ERROR_NONE;
974 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
976 app_tts_state_e state;
977 if (0 > ttsd_data_get_client_state(uid, &state)) {
978 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
979 return TTSD_ERROR_INVALID_PARAMETER;
982 /* get current voice */
983 int ret = ttsd_engine_get_default_voice(language, voice_type);
985 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
989 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
991 return TTSD_ERROR_NONE;
994 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
996 app_tts_state_e state;
997 if (0 > ttsd_data_get_client_state(uid, &state)) {
998 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
999 return TTSD_ERROR_INVALID_PARAMETER;
1002 if (APP_STATE_READY != state) {
1003 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1004 return TTSD_ERROR_INVALID_STATE;
1007 int ret = ttsd_engine_set_private_data(key, data);
1009 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1011 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1017 int ttsd_server_get_private_data(int uid, const char* key, 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_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1027 return TTSD_ERROR_INVALID_STATE;
1030 int ret = ttsd_engine_get_private_data(key, data);
1032 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1034 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1040 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1042 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1045 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1047 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1053 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1055 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1058 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1060 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1066 int ttsd_server_play_pcm(int uid)
1068 app_tts_state_e state;
1069 if (0 > ttsd_data_get_client_state(uid, &state)) {
1070 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1071 return TTSD_ERROR_INVALID_PARAMETER;
1074 if (APP_STATE_PLAYING == state) {
1075 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1076 return TTSD_ERROR_NONE;
1079 int current_uid = ttsd_data_get_current_playing();
1080 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1082 if (uid != current_uid && -1 != current_uid) {
1083 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1084 /* Send interrupt message */
1085 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1088 if (0 != ttsd_server_stop(current_uid)) {
1089 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1091 if (0 != ttsd_player_stop(current_uid)) {
1092 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1095 intptr_t pcurrent_uid = (intptr_t)current_uid;
1096 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1098 /* Default mode policy of interrupt is "Pause" */
1100 /* Send interrupt message */
1101 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1104 if (0 != ttsd_player_pause(current_uid)) {
1105 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1109 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1111 intptr_t pcurrent_uid = (intptr_t)current_uid;
1112 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1116 /* Change current play */
1117 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1118 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1119 return TTSD_ERROR_OPERATION_FAILED;
1122 if (APP_STATE_PAUSED == state) {
1123 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1126 if (0 != ttsd_player_resume(uid)) {
1127 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1130 if (0 != ttsd_player_play_pcm(uid)) {
1131 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1133 // Change ready state
1134 ttsd_server_stop_pcm(uid);
1137 tmp_pid = ttsd_data_get_pid(uid);
1138 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1142 return TTSD_ERROR_NONE;
1145 int ttsd_server_stop_pcm(int uid)
1147 app_tts_state_e state;
1148 if (0 > ttsd_data_get_client_state(uid, &state)) {
1149 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1150 return TTSD_ERROR_INVALID_PARAMETER;
1153 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1155 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1156 ttsd_data_set_client_state(uid, APP_STATE_READY);
1159 /* Reset all data */
1160 ttsd_data_clear_data(uid);
1162 ttsd_player_stop(uid);
1165 return TTSD_ERROR_NONE;
1168 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1170 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1174 /* Synthesis is success */
1175 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1176 if (TTSE_RESULT_EVENT_START == event) {
1177 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1178 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1179 uid, uttid, data, data_size, audio_type, rate);
1180 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1181 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
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);
1185 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1188 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1189 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1190 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1191 return TTSD_ERROR_INVALID_PARAMETER;
1195 sound_data_s* temp_sound_data = NULL;
1196 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1197 if (NULL == temp_sound_data) {
1198 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1199 return TTSD_ERROR_OUT_OF_MEMORY;
1202 temp_sound_data->data = NULL;
1203 temp_sound_data->rate = 0;
1204 temp_sound_data->data_size = 0;
1206 if (0 < data_size) {
1207 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1208 if (NULL != temp_sound_data->data) {
1209 memcpy(temp_sound_data->data, data, data_size);
1210 temp_sound_data->data_size = data_size;
1211 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1212 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1214 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1217 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1220 temp_sound_data->utt_id = uttid;
1221 temp_sound_data->event = event;
1222 temp_sound_data->audio_type = audio_type;
1223 temp_sound_data->rate = rate;
1225 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1226 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1228 if (NULL != temp_sound_data->data) {
1229 free(temp_sound_data->data);
1230 temp_sound_data->data = NULL;
1233 free(temp_sound_data);
1234 temp_sound_data = NULL;
1237 /* if (0 != ttsd_player_play(uid)) {
1238 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1240 // Change ready state
1241 ttsd_server_stop(uid);
1244 tmp_pid = ttsd_data_get_pid(uid);
1245 ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1249 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1252 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1254 return TTSD_ERROR_NONE;