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_data_clear_data(uid))
177 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
180 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
181 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
184 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
185 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
188 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
189 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
194 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)
196 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
198 unsigned int uid = g_utt.uid;
199 int uttid = g_utt.uttid;
201 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
202 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
203 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
204 SLOG(LOG_DEBUG, tts_tag(), "@@@");
205 return TTSD_ERROR_INVALID_PARAMETER;
208 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
209 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
210 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
211 return TTSD_ERROR_INVALID_PARAMETER;
214 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
215 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
216 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
217 SLOG(LOG_DEBUG, tts_tag(), "@@@");
218 return TTSD_ERROR_INVALID_PARAMETER;
221 /* check the current state */
222 app_tts_state_e state = ttsd_data_get_client_state(uid);
223 if (APP_STATE_NONE == state) {
224 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
225 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
226 return TTSD_ERROR_INVALID_PARAMETER;
229 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
230 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
231 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
232 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
233 return TTSD_ERROR_NONE;
236 /* Synthesis is success */
237 if (TTSE_RESULT_EVENT_START == event) {
238 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
239 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
240 uid, uttid, data, data_size, audio_type, rate);
242 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
243 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
244 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
245 double d_latency = (double)latency;
246 g_avg_latency += d_latency;
247 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
248 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
250 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);
251 } else if (TTSE_RESULT_EVENT_FINISH == event) {
252 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
253 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
254 uid, uttid, data, data_size, audio_type, rate);
256 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
260 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
261 if (NULL == sound_data) {
262 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
263 return TTSD_ERROR_OUT_OF_MEMORY;
266 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
267 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
268 ttsd_data_destroy_sound_data(sound_data);
270 return TTSD_ERROR_OPERATION_FAILED;
273 if (event == TTSE_RESULT_EVENT_FINISH) {
274 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
277 return TTSD_ERROR_NONE;
280 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
282 /* clear client data */
283 ttsd_data_clear_data(uid);
285 if (APP_STATE_READY != state) {
286 ttsd_data_set_client_state(uid, APP_STATE_READY);
289 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
291 ttsd_data_delete_client(uid);
298 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
301 case TTS_CONFIG_TYPE_ENGINE:
303 /* TODO - Determine the policy when engine process get engine changed cb */
304 if (NULL == str_param) {
305 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
310 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
311 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
312 ret = ttsd_engine_agent_unload_current_engine();
314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
317 ret = ttsd_engine_agent_load_current_engine();
319 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
324 /* stop all player */
325 ttsd_player_all_stop();
327 /* send interrupt message to all clients */
328 ttsd_data_foreach_clients(__get_client_cb, NULL);
330 ttsd_engine_cancel_synthesis();
335 case TTS_CONFIG_TYPE_VOICE:
337 if (NULL == str_param) {
338 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
342 char* out_lang = NULL;
346 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
347 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
348 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
350 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
352 /* Current language is not available */
353 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
355 if (NULL != out_lang) {
362 case TTS_CONFIG_TYPE_SPEED:
364 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
365 /* set default speed */
367 ret = ttsd_engine_agent_set_default_speed(int_param);
369 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
375 case TTS_CONFIG_TYPE_PITCH:
377 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
378 /* set default speed */
380 ret = ttsd_engine_agent_set_default_pitch(int_param);
382 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
388 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
390 if (0.0 <= double_param && double_param <= 1.0) {
391 /* set default bg volume ratio */
393 ret = ttsd_player_set_background_volume_ratio(double_param);
395 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
405 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
407 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
408 ttsd_server_finalize(uid);
412 static int __terminate_server()
414 if (g_is_terminated) {
415 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
416 return TTSD_ERROR_NONE;
419 int ret = TTSD_ERROR_NONE;
420 ret = ttsd_ipc_close_connection();
421 if (TTSD_ERROR_NONE != ret) {
422 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
423 return TTSD_ERROR_OPERATION_FAILED;
426 ret = ttsd_finalize();
427 if (TTSD_ERROR_NONE != ret) {
428 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
429 return TTSD_ERROR_OPERATION_FAILED;
432 g_is_terminated = true;
433 return TTSD_ERROR_NONE;
436 static Eina_Bool __quit_ecore_loop(void *data)
438 __terminate_server();
439 ecore_main_loop_quit();
444 void __screen_reader_changed_cb(bool value)
446 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
450 int ttsd_send_all_stop()
452 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
454 /* stop all player */
455 ttsd_player_all_stop();
457 /* send interrupt message to all clients */
458 ttsd_data_foreach_clients(__get_client_cb, NULL);
460 return TTSD_ERROR_NONE;
466 int ttsd_initialize(ttse_request_callback_s *callback)
468 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
469 g_is_terminated = false;
471 if (ttsd_config_initialize(__config_changed_cb)) {
472 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
476 if (ttsd_player_init()) {
477 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
478 return TTSD_ERROR_OPERATION_FAILED;
482 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
484 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
485 int ret = ttsd_player_set_background_volume_ratio(ratio);
487 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
489 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
492 /* Engine Agent initialize */
493 if (0 != ttsd_engine_agent_init()) {
494 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
495 return TTSD_ERROR_OPERATION_FAILED;
498 /* set current engine */
499 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
500 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
501 // g_is_engine = false;
503 // g_is_engine = true;
505 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
506 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
507 return TTSD_ERROR_OPERATION_FAILED;
510 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
512 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
513 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
516 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
517 if (NULL == g_check_client_timer) {
518 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
521 return TTSD_ERROR_NONE;
526 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
529 if (0 < g_list_length(g_proc_list)) {
530 iter = g_list_first(g_proc_list);
531 while (NULL != iter) {
532 g_proc_list = g_list_remove_link(g_proc_list, iter);
534 iter = g_list_first(g_proc_list);
539 ecore_timer_del(g_wait_timer);
541 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
544 if (g_check_client_timer) {
545 ecore_timer_del(g_check_client_timer);
546 g_check_client_timer = NULL;
547 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
550 if (g_quit_loop_timer) {
551 ecore_timer_del(g_quit_loop_timer);
552 g_quit_loop_timer = NULL;
553 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
556 ttsd_config_finalize();
558 int ret = TTSD_ERROR_NONE;
559 ret = ttsd_player_release();
560 if (TTSD_ERROR_NONE != ret) {
561 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
562 return TTSD_ERROR_OPERATION_FAILED;
565 ret = ttsd_engine_agent_release();
566 if (TTSD_ERROR_NONE != ret) {
567 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
568 return TTSD_ERROR_OPERATION_FAILED;
571 return TTSD_ERROR_NONE;
576 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
577 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
578 if (TTSD_ERROR_NONE != ret) {
579 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
580 return TTSD_ERROR_OPERATION_FAILED;
583 if (g_quit_loop_timer) {
584 ecore_timer_del(g_quit_loop_timer);
585 g_quit_loop_timer = NULL;
586 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
589 return __terminate_server();
593 * TTS Server Functions for Client
596 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
598 if (-1 != ttsd_data_is_client(uid))
599 *is_initialized = true;
601 *is_initialized = false;
603 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
604 return TTSD_ERROR_NONE;
607 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
609 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
611 if (-1 != ttsd_data_is_client(uid)) {
612 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
613 return TTSD_ERROR_NONE;
616 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
617 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
618 return TTSD_ERROR_OPERATION_FAILED;
621 if (true == *credential_needed) {
623 if (0 != app_manager_get_app_id(pid, &appid)) {
624 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
626 bool is_agreed = false;
627 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
628 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
631 return TTSD_ERROR_OPERATION_FAILED;
636 if (false == is_agreed) {
637 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
638 return TTSD_ERROR_PERMISSION_DENIED;
642 if (0 != ttsd_data_new_client(pid, uid)) {
643 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
644 return TTSD_ERROR_OPERATION_FAILED;
647 if (0 != ttsd_data_set_ipc_method(uid, method)) {
648 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
649 return TTSD_ERROR_OPERATION_FAILED;
652 return TTSD_ERROR_NONE;
655 static void __read_proc()
658 struct dirent *dirp = NULL;
662 if (0 < g_list_length(g_proc_list)) {
663 iter = g_list_first(g_proc_list);
664 while (NULL != iter) {
665 g_proc_list = g_list_remove_link(g_proc_list, iter);
667 iter = g_list_first(g_proc_list);
671 dp = opendir("/proc");
673 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
679 tmp = atoi(dirp->d_name);
680 if (0 >= tmp) continue;
681 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
683 } while (NULL != dirp);
689 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
695 for (i = 0; i < g_list_length(g_proc_list); i++) {
696 iter = g_list_nth(g_proc_list, i);
698 if (pid == GPOINTER_TO_INT(iter->data)) {
699 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
706 if (false == exist) {
707 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
708 ttsd_server_finalize(uid);
713 char appid[1024] = {0, };
714 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
715 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
718 if (0 < strlen(appid)) {
719 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
721 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
724 result = ttsdc_send_hello(pid, uid);
727 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
728 ttsd_server_finalize(uid);
729 } else if (-1 == result) {
730 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
738 Eina_Bool ttsd_cleanup_client(void *data)
740 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
743 if (0 < ttsd_data_get_client_count()) {
744 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
746 if (NULL == g_quit_loop_timer) {
747 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
748 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
750 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
751 g_check_client_timer = NULL;
755 SLOG(LOG_DEBUG, tts_tag(), "@@@");
760 void __used_voice_cb(const char* lang, int type)
762 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
763 if (0 != ttsd_engine_unload_voice(lang, type)) {
764 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
768 int ttsd_server_finalize(unsigned int uid)
770 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
771 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);
773 if (0 > ttsd_data_is_client(uid)) {
774 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
777 ttsd_server_stop(uid);
779 /* Need to unload voice when used voice is unregistered */
780 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
781 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
782 return TTSD_ERROR_OPERATION_FAILED;
785 ttsd_data_delete_client(uid);
787 /* unload engine, if ref count of client is 0 */
788 if (0 == ttsd_data_get_client_count()) {
789 if (NULL == g_quit_loop_timer) {
790 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
791 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
793 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
797 return TTSD_ERROR_NONE;
800 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
802 app_tts_state_e state = ttsd_data_get_client_state(uid);
803 if (APP_STATE_NONE == state) {
804 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
805 return TTSD_ERROR_INVALID_PARAMETER;
808 if (APP_STATE_READY != state) {
809 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
810 return TTSD_ERROR_INVALID_STATE;
813 int ret = ttsd_data_set_mode(uid, mode);
815 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
817 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
823 static bool __is_connected_to_network()
826 int network_status = 0;
827 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
829 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
833 if (network_status == VCONFKEY_NETWORK_OFF) {
834 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
838 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
843 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)
845 app_tts_state_e state = ttsd_data_get_client_state(uid);
846 if (APP_STATE_NONE == state) {
847 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
848 return TTSD_ERROR_INVALID_PARAMETER;
851 /* check valid voice */
852 char* temp_lang = NULL;
854 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
855 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
856 if (NULL != temp_lang) {
860 return TTSD_ERROR_INVALID_VOICE;
863 if (NULL == temp_lang) {
864 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
865 return TTSD_ERROR_INVALID_VOICE;
868 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
869 if (NULL == speak_data) {
870 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
871 if (NULL != temp_lang) {
876 return TTSD_ERROR_OPERATION_FAILED;
879 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);
881 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
883 ret = ttsd_data_add_speak_data(uid, speak_data);
885 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
886 if (NULL != temp_lang) {
891 ttsd_data_destroy_speak_data(speak_data);
897 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
898 /* Request load voice */
899 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
900 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
901 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
905 if (NULL != temp_lang) {
910 if (APP_STATE_PLAYING == state) {
911 /* check if engine use network */
912 if (ttsd_engine_agent_need_network()) {
913 if (false == __is_connected_to_network()) {
914 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
915 return TTSD_ERROR_OPERATION_FAILED;
919 ttsd_data_set_credential(uid, credential);
921 /* Check whether tts-engine is running or not */
922 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
923 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
924 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
925 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
931 return TTSD_ERROR_NONE;
934 static int __pause_client(unsigned int uid)
936 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
937 if (TTSD_ERROR_NONE != ret) {
938 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
939 return TTSD_ERROR_OPERATION_FAILED;
942 ret = ttsd_player_pause(uid);
943 if (TTSD_ERROR_NONE != ret) {
944 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
945 return TTSD_ERROR_OPERATION_FAILED;
948 return TTSD_ERROR_NONE;
951 void __send_interrupt_client(unsigned int uid)
953 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
954 int pid = ttsd_data_get_pid(uid);
956 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
960 app_tts_state_e state = ttsd_data_get_client_state(uid);
961 if (APP_STATE_NONE == state) {
962 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
966 ttsdc_ipc_send_set_state_message(pid, uid, state);
967 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
971 int ttsd_server_play(unsigned int uid, const char* credential)
973 app_tts_state_e state = ttsd_data_get_client_state(uid);
974 if (APP_STATE_NONE == state) {
975 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
976 return TTSD_ERROR_INVALID_PARAMETER;
979 if (APP_STATE_PLAYING == state) {
980 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
981 return TTSD_ERROR_NONE;
984 /* check if engine use network */
985 if (ttsd_engine_agent_need_network()) {
986 if (false == __is_connected_to_network()) {
987 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
988 return TTSD_ERROR_OUT_OF_NETWORK;
992 /* check the current playback focus */
993 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
994 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
995 ttsd_data_clear_data(uid);
996 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
999 unsigned int current_uid = ttsd_data_get_current_playing();
1000 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1002 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1003 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1004 /* Send interrupt message */
1005 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1008 if (0 != ttsd_server_stop(current_uid)) {
1009 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1012 /* Default mode policy of interrupt is "Pause" */
1014 /* Send interrupt message */
1015 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1018 if (0 != __pause_client(current_uid)) {
1019 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1023 __send_interrupt_client(current_uid);
1026 /* Change current play */
1027 if (0 != ttsd_player_wait_to_play(uid)) {
1028 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1031 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1032 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1033 return TTSD_ERROR_OPERATION_FAILED;
1036 int ret = TTSD_ERROR_NONE;
1037 if (APP_STATE_PAUSED == state) {
1038 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1039 g_is_paused = false;
1040 ret = ttsd_player_resume(uid);
1042 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1043 ret = ttsd_player_play(uid);
1046 if (TTSD_ERROR_NONE != ret) {
1047 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1048 ttsd_data_set_client_state(uid, state);
1049 return TTSD_ERROR_OPERATION_FAILED;
1052 ttsd_data_set_credential(uid, credential);
1054 /* Check whether tts-engine is running or not */
1055 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1056 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1057 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1058 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1059 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1064 return TTSD_ERROR_NONE;
1067 int ttsd_server_stop(unsigned int uid)
1069 app_tts_state_e state = ttsd_data_get_client_state(uid);
1070 if (APP_STATE_NONE == state) {
1071 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1072 return TTSD_ERROR_INVALID_PARAMETER;
1075 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1077 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1078 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1079 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1080 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1081 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1083 int ret = ttsd_engine_cancel_synthesis();
1084 if (TTSD_ERROR_NONE != ret)
1085 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1087 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1090 ttsd_data_set_client_state(uid, APP_STATE_READY);
1091 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1092 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1096 /* Reset all data */
1097 ttsd_data_clear_data(uid);
1098 g_is_paused = false;
1100 return TTSD_ERROR_NONE;
1103 int ttsd_server_pause(unsigned int uid, int* utt_id)
1105 app_tts_state_e state = ttsd_data_get_client_state(uid);
1106 if (APP_STATE_NONE == state) {
1107 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1108 return TTSD_ERROR_INVALID_PARAMETER;
1111 if (APP_STATE_PLAYING != state) {
1112 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1113 return TTSD_ERROR_INVALID_STATE;
1118 *utt_id = g_utt.uttid;
1119 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1121 int ret = __pause_client(uid);
1122 if (TTSD_ERROR_NONE != ret) {
1123 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1124 return TTSD_ERROR_OPERATION_FAILED;
1127 return TTSD_ERROR_NONE;
1130 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1132 if (0 > ttsd_data_is_client(uid)) {
1133 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1134 return TTSD_ERROR_INVALID_PARAMETER;
1138 int ret = ttsd_engine_get_voice_list(voice_list);
1140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1144 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1146 return TTSD_ERROR_NONE;
1149 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1151 if (0 > ttsd_data_is_client(uid)) {
1152 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1153 return TTSD_ERROR_INVALID_PARAMETER;
1156 /* get current voice */
1157 int ret = ttsd_engine_get_default_voice(language, voice_type);
1159 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1163 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1165 return TTSD_ERROR_NONE;
1168 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1170 app_tts_state_e state = ttsd_data_get_client_state(uid);
1171 if (APP_STATE_NONE == state) {
1172 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1173 return TTSD_ERROR_INVALID_PARAMETER;
1176 if (APP_STATE_READY != state) {
1177 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1178 return TTSD_ERROR_INVALID_STATE;
1181 int ret = ttsd_engine_set_private_data(key, data);
1183 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1185 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1191 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1193 app_tts_state_e state = ttsd_data_get_client_state(uid);
1194 if (APP_STATE_NONE == state) {
1195 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1196 return TTSD_ERROR_INVALID_PARAMETER;
1199 if (APP_STATE_READY != state) {
1200 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1201 return TTSD_ERROR_INVALID_STATE;
1204 int ret = ttsd_engine_get_private_data(key, data);
1206 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1208 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1214 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1216 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1219 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1221 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1227 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1229 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1232 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1234 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1240 int ttsd_server_play_pcm(unsigned int uid)
1242 app_tts_state_e state = ttsd_data_get_client_state(uid);
1243 if (APP_STATE_NONE == state) {
1244 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1245 return TTSD_ERROR_INVALID_PARAMETER;
1248 if (APP_STATE_PLAYING == state) {
1249 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1250 return TTSD_ERROR_NONE;
1253 unsigned int current_uid = ttsd_data_get_current_playing();
1254 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1256 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1257 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1258 /* Send interrupt message */
1259 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1262 if (0 != ttsd_server_stop(current_uid)) {
1263 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1266 /* Default mode policy of interrupt is "Pause" */
1268 /* Send interrupt message */
1269 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1272 if (0 != __pause_client(current_uid)) {
1273 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1277 __send_interrupt_client(current_uid);
1280 /* Change current play */
1281 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1282 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1283 return TTSD_ERROR_OPERATION_FAILED;
1286 int ret = TTSD_ERROR_NONE;
1287 if (APP_STATE_PAUSED == state) {
1288 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1289 g_is_paused = false;
1290 ret = ttsd_player_resume(uid);
1292 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1293 ret = ttsd_player_play(uid);
1296 if (TTSD_ERROR_NONE != ret) {
1297 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1298 ttsd_data_set_client_state(uid, state);
1299 return TTSD_ERROR_OPERATION_FAILED;
1302 return TTSD_ERROR_NONE;
1305 int ttsd_server_stop_pcm(unsigned int uid)
1307 app_tts_state_e state = ttsd_data_get_client_state(uid);
1308 if (APP_STATE_NONE == state) {
1309 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1310 return TTSD_ERROR_INVALID_PARAMETER;
1313 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1315 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1316 ttsd_data_set_client_state(uid, APP_STATE_READY);
1319 /* Reset all data */
1320 ttsd_data_clear_data(uid);
1322 ttsd_player_stop(uid);
1324 return TTSD_ERROR_NONE;
1327 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1329 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1333 /* Synthesis is success */
1334 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1335 if (TTSE_RESULT_EVENT_START == event) {
1336 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1337 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1338 uid, uttid, data, data_size, audio_type, rate);
1339 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1340 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1341 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1342 uid, uttid, data, data_size, audio_type, rate);
1344 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1347 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1348 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1349 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1350 return TTSD_ERROR_INVALID_PARAMETER;
1353 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1354 if (NULL == sound_data) {
1355 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1356 return TTSD_ERROR_OUT_OF_MEMORY;
1359 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1360 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1361 ttsd_data_destroy_sound_data(sound_data);
1364 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1367 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1369 return TTSD_ERROR_NONE;