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>
19 #include "ttsd_config.h"
20 #include "ttsd_data.h"
21 #include "ttsd_dbus.h"
22 #include "ttsd_dbus_server.h"
24 #include "ttsd_engine_agent.h"
25 #include "ttsd_main.h"
26 #include "ttsd_player.h"
27 #include "ttsd_server.h"
30 #define CLIENT_CLEAN_UP_TIME 500
32 /* for checking time */
33 #define TIME_DIFF(start, end) \
34 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
36 static struct timespec g_request_playing, g_start_playing;
37 static double g_avg_latency;
38 static double g_min_latency;
39 static double g_max_latency;
40 static int g_file_num;
47 /* If current engine exist */
48 //static bool g_is_engine;
50 static Ecore_Timer* g_check_client_timer = NULL;
51 static Ecore_Timer* g_wait_timer = NULL;
52 static Ecore_Timer* g_quit_loop_timer = NULL;
54 static utterance_t g_utt;
56 static GList *g_proc_list = NULL;
58 static bool g_is_paused;
60 static bool g_is_terminated = false;
63 /* Function definitions */
64 static void __synthesis(unsigned int uid);
66 static Eina_Bool __wait_synthesis(void *data)
68 /* get current play */
69 unsigned int uid = ttsd_data_get_current_playing();
72 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
73 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
76 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
78 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
79 /* Start next synthesis */
90 static void __synthesis(unsigned int uid)
92 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
94 speak_data_s* speak_data = NULL;
95 int ret = ttsd_data_get_speak_data(uid, &speak_data);
96 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
97 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
98 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
102 int pid = ttsd_data_get_pid(uid);
104 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
105 ttsd_data_destroy_speak_data(speak_data);
110 char appid[1024] = {0, };
111 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
112 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
115 if (NULL == speak_data->lang || NULL == speak_data->text) {
116 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
117 ttsd_server_stop(uid);
118 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
119 ttsd_data_destroy_speak_data(speak_data);
125 g_utt.uttid = speak_data->utt_id;
127 char* credential = ttsd_data_get_credential(uid);
129 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
130 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
131 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
132 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
133 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
134 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
136 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
137 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
138 if (TTSD_ERROR_NONE != ret) {
139 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
141 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
142 ttsd_server_stop(uid);
144 int pid = ttsd_data_get_pid(uid);
146 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
148 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
151 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
156 ttsd_data_destroy_speak_data(speak_data);
158 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
164 * TTS Server Callback Functions
166 int ttsd_send_error(ttse_error_e error, const char* msg)
168 unsigned int uid = g_utt.uid;
169 int uttid = g_utt.uttid;
170 int tmp_pid = ttsd_data_get_pid(uid);
172 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%u), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
174 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
176 if (0 != ttsd_player_clear(uid))
177 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
179 if (0 != ttsd_data_clear_data(uid))
180 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
183 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
184 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
187 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
188 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
191 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
192 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
197 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)
199 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
201 unsigned int uid = g_utt.uid;
202 int uttid = g_utt.uttid;
204 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
205 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
206 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
207 SLOG(LOG_DEBUG, tts_tag(), "@@@");
208 return TTSD_ERROR_INVALID_PARAMETER;
211 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
212 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
213 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
214 return TTSD_ERROR_INVALID_PARAMETER;
217 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
218 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
219 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
220 SLOG(LOG_DEBUG, tts_tag(), "@@@");
221 return TTSD_ERROR_INVALID_PARAMETER;
224 /* check the current state */
225 app_tts_state_e state = ttsd_data_get_client_state(uid);
226 if (APP_STATE_NONE == state) {
227 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
228 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
229 return TTSD_ERROR_INVALID_PARAMETER;
232 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
233 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
234 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
235 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
236 return TTSD_ERROR_NONE;
239 /* Synthesis is success */
240 if (TTSE_RESULT_EVENT_START == event) {
241 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
242 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
243 uid, uttid, data, data_size, audio_type, rate);
245 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
246 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
247 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
248 double d_latency = (double)latency;
249 g_avg_latency += d_latency;
250 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
251 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
253 SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
254 } else if (TTSE_RESULT_EVENT_FINISH == event) {
255 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
256 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
257 uid, uttid, data, data_size, audio_type, rate);
259 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
263 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
264 if (NULL == sound_data) {
265 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
266 return TTSD_ERROR_OUT_OF_MEMORY;
269 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
270 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
271 ttsd_data_destroy_sound_data(sound_data);
273 return TTSD_ERROR_OPERATION_FAILED;
276 if (event == TTSE_RESULT_EVENT_FINISH) {
277 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
280 return TTSD_ERROR_NONE;
283 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
285 /* clear client data */
286 ttsd_data_clear_data(uid);
288 if (APP_STATE_READY != state) {
289 ttsd_data_set_client_state(uid, APP_STATE_READY);
292 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
294 ttsd_data_delete_client(uid);
301 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
304 case TTS_CONFIG_TYPE_ENGINE:
306 /* TODO - Determine the policy when engine process get engine changed cb */
307 if (NULL == str_param) {
308 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
313 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
314 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
315 ret = ttsd_engine_agent_unload_current_engine();
317 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
320 ret = ttsd_engine_agent_load_current_engine();
322 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
327 /* stop all player */
328 ttsd_player_all_stop();
330 /* send interrupt message to all clients */
331 ttsd_data_foreach_clients(__get_client_cb, NULL);
333 ttsd_engine_cancel_synthesis();
338 case TTS_CONFIG_TYPE_VOICE:
340 if (NULL == str_param) {
341 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
345 char* out_lang = NULL;
349 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
350 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
351 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
353 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
355 /* Current language is not available */
356 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
358 if (NULL != out_lang) {
365 case TTS_CONFIG_TYPE_SPEED:
367 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
368 /* set default speed */
370 ret = ttsd_engine_agent_set_default_speed(int_param);
372 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
378 case TTS_CONFIG_TYPE_PITCH:
380 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
381 /* set default speed */
383 ret = ttsd_engine_agent_set_default_pitch(int_param);
385 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
391 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
393 if (0.0 <= double_param && double_param <= 1.0) {
394 /* set default bg volume ratio */
396 ret = ttsd_player_set_background_volume_ratio(double_param);
398 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
408 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
410 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
411 ttsd_server_finalize(uid);
415 static int __terminate_server()
417 if (g_is_terminated) {
418 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
419 return TTSD_ERROR_NONE;
422 int ret = TTSD_ERROR_NONE;
423 ret = ttsd_ipc_close_connection();
424 if (TTSD_ERROR_NONE != ret) {
425 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
426 return TTSD_ERROR_OPERATION_FAILED;
429 ret = ttsd_finalize();
430 if (TTSD_ERROR_NONE != ret) {
431 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
432 return TTSD_ERROR_OPERATION_FAILED;
435 g_is_terminated = true;
436 return TTSD_ERROR_NONE;
439 static Eina_Bool __quit_ecore_loop(void *data)
441 __terminate_server();
442 ecore_main_loop_quit();
447 void __screen_reader_changed_cb(bool value)
449 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
453 int ttsd_send_all_stop()
455 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
457 /* stop all player */
458 ttsd_player_all_stop();
460 /* send interrupt message to all clients */
461 ttsd_data_foreach_clients(__get_client_cb, NULL);
463 return TTSD_ERROR_NONE;
469 int ttsd_initialize(ttse_request_callback_s *callback)
471 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
472 g_is_terminated = false;
474 if (ttsd_config_initialize(__config_changed_cb)) {
475 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
479 if (ttsd_player_init()) {
480 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
481 return TTSD_ERROR_OPERATION_FAILED;
485 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
487 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
488 int ret = ttsd_player_set_background_volume_ratio(ratio);
490 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
492 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
495 /* Engine Agent initialize */
496 if (0 != ttsd_engine_agent_init()) {
497 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
498 return TTSD_ERROR_OPERATION_FAILED;
501 /* set current engine */
502 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
503 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
504 // g_is_engine = false;
506 // g_is_engine = true;
508 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
509 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
510 return TTSD_ERROR_OPERATION_FAILED;
513 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
515 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
516 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
519 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
520 if (NULL == g_check_client_timer) {
521 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
524 return TTSD_ERROR_NONE;
529 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
532 if (0 < g_list_length(g_proc_list)) {
533 iter = g_list_first(g_proc_list);
534 while (NULL != iter) {
535 g_proc_list = g_list_remove_link(g_proc_list, iter);
537 iter = g_list_first(g_proc_list);
542 ecore_timer_del(g_wait_timer);
544 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
547 if (g_check_client_timer) {
548 ecore_timer_del(g_check_client_timer);
549 g_check_client_timer = NULL;
550 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
553 if (g_quit_loop_timer) {
554 ecore_timer_del(g_quit_loop_timer);
555 g_quit_loop_timer = NULL;
556 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
559 ttsd_config_finalize();
561 int ret = TTSD_ERROR_NONE;
562 ret = ttsd_player_release();
563 if (TTSD_ERROR_NONE != ret) {
564 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
565 return TTSD_ERROR_OPERATION_FAILED;
568 ret = ttsd_engine_agent_release();
569 if (TTSD_ERROR_NONE != ret) {
570 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
571 return TTSD_ERROR_OPERATION_FAILED;
574 return TTSD_ERROR_NONE;
579 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
580 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
581 if (TTSD_ERROR_NONE != ret) {
582 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
583 return TTSD_ERROR_OPERATION_FAILED;
586 if (g_quit_loop_timer) {
587 ecore_timer_del(g_quit_loop_timer);
588 g_quit_loop_timer = NULL;
589 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
592 return __terminate_server();
596 * TTS Server Functions for Client
599 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
601 if (-1 != ttsd_data_is_client(uid))
602 *is_initialized = true;
604 *is_initialized = false;
606 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
607 return TTSD_ERROR_NONE;
610 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
612 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
614 if (-1 != ttsd_data_is_client(uid)) {
615 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
616 return TTSD_ERROR_NONE;
619 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
620 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
621 return TTSD_ERROR_OPERATION_FAILED;
624 if (true == *credential_needed) {
626 if (0 != app_manager_get_app_id(pid, &appid)) {
627 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
629 bool is_agreed = false;
630 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
631 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
634 return TTSD_ERROR_OPERATION_FAILED;
639 if (false == is_agreed) {
640 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
641 return TTSD_ERROR_PERMISSION_DENIED;
645 if (0 != ttsd_data_new_client(pid, uid)) {
646 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
647 return TTSD_ERROR_OPERATION_FAILED;
650 if (0 != ttsd_data_set_ipc_method(uid, method)) {
651 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
652 return TTSD_ERROR_OPERATION_FAILED;
655 if (0 != ttsd_player_create_instance(uid)) {
656 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
657 return TTSD_ERROR_OPERATION_FAILED;
661 g_min_latency = 10000.0;
666 return TTSD_ERROR_NONE;
669 static void __read_proc()
672 struct dirent *dirp = NULL;
676 if (0 < g_list_length(g_proc_list)) {
677 iter = g_list_first(g_proc_list);
678 while (NULL != iter) {
679 g_proc_list = g_list_remove_link(g_proc_list, iter);
681 iter = g_list_first(g_proc_list);
685 dp = opendir("/proc");
687 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
693 tmp = atoi(dirp->d_name);
694 if (0 >= tmp) continue;
695 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
697 } while (NULL != dirp);
703 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
709 for (i = 0; i < g_list_length(g_proc_list); i++) {
710 iter = g_list_nth(g_proc_list, i);
712 if (pid == GPOINTER_TO_INT(iter->data)) {
713 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
720 if (false == exist) {
721 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
722 ttsd_server_finalize(uid);
727 char appid[1024] = {0, };
728 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
729 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
732 if (0 < strlen(appid)) {
733 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
735 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
738 result = ttsdc_send_hello(pid, uid);
741 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
742 ttsd_server_finalize(uid);
743 } else if (-1 == result) {
744 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
752 Eina_Bool ttsd_cleanup_client(void *data)
754 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
757 if (0 < ttsd_data_get_client_count()) {
758 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
760 if (NULL == g_quit_loop_timer) {
761 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
762 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
764 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
765 g_check_client_timer = NULL;
769 SLOG(LOG_DEBUG, tts_tag(), "@@@");
774 void __used_voice_cb(const char* lang, int type)
776 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
777 if (0 != ttsd_engine_unload_voice(lang, type)) {
778 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
782 int ttsd_server_finalize(unsigned int uid)
784 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
785 SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
787 if (0 > ttsd_data_is_client(uid)) {
788 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
791 ttsd_server_stop(uid);
792 ttsd_player_clear(uid);
794 ttsd_player_destroy_instance(uid);
796 /* Need to unload voice when used voice is unregistered */
797 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
798 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
799 return TTSD_ERROR_OPERATION_FAILED;
802 ttsd_data_delete_client(uid);
804 /* unload engine, if ref count of client is 0 */
805 if (0 == ttsd_data_get_client_count()) {
806 if (NULL == g_quit_loop_timer) {
807 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
808 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
810 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
814 return TTSD_ERROR_NONE;
817 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
819 app_tts_state_e state = ttsd_data_get_client_state(uid);
820 if (APP_STATE_NONE == state) {
821 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
822 return TTSD_ERROR_INVALID_PARAMETER;
825 if (APP_STATE_READY != state) {
826 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
827 return TTSD_ERROR_INVALID_STATE;
830 int ret = ttsd_data_set_mode(uid, mode);
832 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
834 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
840 static bool __is_connected_to_network()
843 int network_status = 0;
844 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
846 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
850 if (network_status == VCONFKEY_NETWORK_OFF) {
851 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
855 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
860 int ttsd_server_add_queue(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
862 app_tts_state_e state = ttsd_data_get_client_state(uid);
863 if (APP_STATE_NONE == state) {
864 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
865 return TTSD_ERROR_INVALID_PARAMETER;
868 /* check valid voice */
869 char* temp_lang = NULL;
871 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
872 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
873 if (NULL != temp_lang) {
877 return TTSD_ERROR_INVALID_VOICE;
880 if (NULL == temp_lang) {
881 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
882 return TTSD_ERROR_INVALID_VOICE;
885 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
886 if (NULL == speak_data) {
887 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
888 if (NULL != temp_lang) {
893 return TTSD_ERROR_OPERATION_FAILED;
896 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);
898 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
900 ret = ttsd_data_add_speak_data(uid, speak_data);
902 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
903 if (NULL != temp_lang) {
908 ttsd_data_destroy_speak_data(speak_data);
914 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
915 /* Request load voice */
916 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
917 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
918 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
922 if (NULL != temp_lang) {
927 if (APP_STATE_PLAYING == state) {
928 /* check if engine use network */
929 if (ttsd_engine_agent_need_network()) {
930 if (false == __is_connected_to_network()) {
931 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
932 return TTSD_ERROR_OPERATION_FAILED;
936 ttsd_data_set_credential(uid, credential);
938 /* Check whether tts-engine is running or not */
939 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
940 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
941 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
942 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
948 return TTSD_ERROR_NONE;
951 static int __pause_client(unsigned int uid)
953 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
954 if (TTSD_ERROR_NONE != ret) {
955 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
956 return TTSD_ERROR_OPERATION_FAILED;
959 ret = ttsd_player_pause(uid);
960 if (TTSD_ERROR_NONE != ret) {
961 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
962 return TTSD_ERROR_OPERATION_FAILED;
965 return TTSD_ERROR_NONE;
968 void __send_interrupt_client(unsigned int uid)
970 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
971 int pid = ttsd_data_get_pid(uid);
973 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
977 app_tts_state_e state = ttsd_data_get_client_state(uid);
978 if (APP_STATE_NONE == state) {
979 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
983 ttsdc_ipc_send_set_state_message(pid, uid, state);
984 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
988 int ttsd_server_play(unsigned int uid, const char* credential)
990 app_tts_state_e state = ttsd_data_get_client_state(uid);
991 if (APP_STATE_NONE == state) {
992 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
993 return TTSD_ERROR_INVALID_PARAMETER;
996 if (APP_STATE_PLAYING == state) {
997 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
998 return TTSD_ERROR_NONE;
1001 /* check if engine use network */
1002 if (ttsd_engine_agent_need_network()) {
1003 if (false == __is_connected_to_network()) {
1004 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1005 return TTSD_ERROR_OUT_OF_NETWORK;
1009 /* check the current playback focus */
1010 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1011 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1012 ttsd_data_clear_data(uid);
1013 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1016 unsigned int current_uid = ttsd_data_get_current_playing();
1017 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1019 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1020 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1021 /* Send interrupt message */
1022 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1025 if (0 != ttsd_server_stop(current_uid)) {
1026 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1029 /* Default mode policy of interrupt is "Pause" */
1031 /* Send interrupt message */
1032 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1035 if (0 != __pause_client(current_uid)) {
1036 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1040 __send_interrupt_client(current_uid);
1043 /* Change current play */
1044 if (0 != ttsd_player_wait_to_play(uid)) {
1045 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1048 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1049 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1050 return TTSD_ERROR_OPERATION_FAILED;
1053 int ret = TTSD_ERROR_NONE;
1054 if (APP_STATE_PAUSED == state) {
1055 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1056 g_is_paused = false;
1057 ret = ttsd_player_resume(uid);
1059 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1060 ret = ttsd_player_play(uid);
1063 if (TTSD_ERROR_NONE != ret) {
1064 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1065 ttsd_data_set_client_state(uid, state);
1066 return TTSD_ERROR_OPERATION_FAILED;
1069 ttsd_data_set_credential(uid, credential);
1071 /* Check whether tts-engine is running or not */
1072 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1073 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1074 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1075 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1076 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1081 return TTSD_ERROR_NONE;
1084 int ttsd_server_stop(unsigned int uid)
1086 app_tts_state_e state = ttsd_data_get_client_state(uid);
1087 if (APP_STATE_NONE == state) {
1088 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1089 return TTSD_ERROR_INVALID_PARAMETER;
1092 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1094 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1095 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1096 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1097 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1098 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1100 int ret = ttsd_engine_cancel_synthesis();
1101 if (TTSD_ERROR_NONE != ret)
1102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1104 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1107 ttsd_data_set_client_state(uid, APP_STATE_READY);
1108 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1109 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1113 /* Reset all data */
1114 ttsd_data_clear_data(uid);
1115 g_is_paused = false;
1117 return TTSD_ERROR_NONE;
1120 int ttsd_server_pause(unsigned int uid, int* utt_id)
1122 app_tts_state_e state = ttsd_data_get_client_state(uid);
1123 if (APP_STATE_NONE == state) {
1124 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1125 return TTSD_ERROR_INVALID_PARAMETER;
1128 if (APP_STATE_PLAYING != state) {
1129 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1130 return TTSD_ERROR_INVALID_STATE;
1135 *utt_id = g_utt.uttid;
1136 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1138 int ret = __pause_client(uid);
1139 if (TTSD_ERROR_NONE != ret) {
1140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1141 return TTSD_ERROR_OPERATION_FAILED;
1144 return TTSD_ERROR_NONE;
1147 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1149 if (0 > ttsd_data_is_client(uid)) {
1150 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1151 return TTSD_ERROR_INVALID_PARAMETER;
1155 int ret = ttsd_engine_get_voice_list(voice_list);
1157 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1161 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1163 return TTSD_ERROR_NONE;
1166 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1168 if (0 > ttsd_data_is_client(uid)) {
1169 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1170 return TTSD_ERROR_INVALID_PARAMETER;
1173 /* get current voice */
1174 int ret = ttsd_engine_get_default_voice(language, voice_type);
1176 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1180 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1182 return TTSD_ERROR_NONE;
1185 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1187 app_tts_state_e state = ttsd_data_get_client_state(uid);
1188 if (APP_STATE_NONE == state) {
1189 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1190 return TTSD_ERROR_INVALID_PARAMETER;
1193 if (APP_STATE_READY != state) {
1194 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1195 return TTSD_ERROR_INVALID_STATE;
1198 int ret = ttsd_engine_set_private_data(key, data);
1200 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1202 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1208 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1210 app_tts_state_e state = ttsd_data_get_client_state(uid);
1211 if (APP_STATE_NONE == state) {
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1213 return TTSD_ERROR_INVALID_PARAMETER;
1216 if (APP_STATE_READY != state) {
1217 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1218 return TTSD_ERROR_INVALID_STATE;
1221 int ret = ttsd_engine_get_private_data(key, data);
1223 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1225 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1231 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1233 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1236 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1238 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1244 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1246 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1249 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1251 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1257 int ttsd_server_play_pcm(unsigned int uid)
1259 app_tts_state_e state = ttsd_data_get_client_state(uid);
1260 if (APP_STATE_NONE == state) {
1261 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1262 return TTSD_ERROR_INVALID_PARAMETER;
1265 if (APP_STATE_PLAYING == state) {
1266 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1267 return TTSD_ERROR_NONE;
1270 unsigned int current_uid = ttsd_data_get_current_playing();
1271 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1273 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1274 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1275 /* Send interrupt message */
1276 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1279 if (0 != ttsd_server_stop(current_uid)) {
1280 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1283 /* Default mode policy of interrupt is "Pause" */
1285 /* Send interrupt message */
1286 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1289 if (0 != __pause_client(current_uid)) {
1290 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1294 __send_interrupt_client(current_uid);
1297 /* Change current play */
1298 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1299 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1300 return TTSD_ERROR_OPERATION_FAILED;
1303 int ret = TTSD_ERROR_NONE;
1304 if (APP_STATE_PAUSED == state) {
1305 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1306 g_is_paused = false;
1307 ret = ttsd_player_resume(uid);
1309 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1310 ret = ttsd_player_play(uid);
1313 if (TTSD_ERROR_NONE != ret) {
1314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1315 ttsd_data_set_client_state(uid, state);
1316 return TTSD_ERROR_OPERATION_FAILED;
1319 return TTSD_ERROR_NONE;
1322 int ttsd_server_stop_pcm(unsigned int uid)
1324 app_tts_state_e state = ttsd_data_get_client_state(uid);
1325 if (APP_STATE_NONE == state) {
1326 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1327 return TTSD_ERROR_INVALID_PARAMETER;
1330 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1332 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1333 ttsd_data_set_client_state(uid, APP_STATE_READY);
1336 /* Reset all data */
1337 ttsd_data_clear_data(uid);
1339 ttsd_player_stop(uid);
1341 return TTSD_ERROR_NONE;
1344 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1346 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1350 /* Synthesis is success */
1351 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1352 if (TTSE_RESULT_EVENT_START == event) {
1353 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1354 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1355 uid, uttid, data, data_size, audio_type, rate);
1356 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1357 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1358 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1359 uid, uttid, data, data_size, audio_type, rate);
1361 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1364 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1365 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1366 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1367 return TTSD_ERROR_INVALID_PARAMETER;
1370 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1371 if (NULL == sound_data) {
1372 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1373 return TTSD_ERROR_OUT_OF_MEMORY;
1376 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1377 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1378 ttsd_data_destroy_sound_data(sound_data);
1381 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1384 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1386 return TTSD_ERROR_NONE;