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_network.h"
27 #include "ttsd_player.h"
28 #include "ttsd_server.h"
31 #define CLIENT_CLEAN_UP_TIME 500
33 /* for checking time */
34 #define TIME_DIFF(start, end) \
35 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
37 static struct timespec g_request_playing, g_start_playing;
38 static double g_avg_latency;
39 static double g_min_latency;
40 static double g_max_latency;
41 static int g_file_num;
48 /* If current engine exist */
49 //static bool g_is_engine;
51 static Ecore_Timer* g_check_client_timer = NULL;
52 static Ecore_Timer* g_wait_timer = NULL;
53 static Ecore_Timer* g_terminate_timer = NULL;
54 static Ecore_Timer* g_quit_loop_timer = NULL;
56 static utterance_t g_utt;
58 static GList *g_proc_list = NULL;
60 static bool g_is_paused;
62 static bool g_is_terminated = false;
65 /* Function definitions */
66 static void __synthesis(unsigned int uid);
68 static Eina_Bool __wait_synthesis(void *data)
70 /* get current play */
71 unsigned int uid = ttsd_data_get_current_playing();
74 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
75 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
78 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
80 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
81 /* Start next synthesis */
92 static void __synthesis(unsigned int uid)
94 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
96 speak_data_s* speak_data = NULL;
97 int ret = ttsd_data_get_speak_data(uid, &speak_data);
98 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
99 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
100 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
104 int pid = ttsd_data_get_pid(uid);
106 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
107 ttsd_data_destroy_speak_data(speak_data);
112 char appid[1024] = {0, };
113 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
114 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
117 if (NULL == speak_data->lang || NULL == speak_data->text) {
118 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
119 ttsd_server_stop(uid);
120 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
121 ttsd_data_destroy_speak_data(speak_data);
127 g_utt.uttid = speak_data->utt_id;
129 char* credential = ttsd_data_get_credential(uid);
131 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
132 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
133 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
134 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
135 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
136 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
138 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
139 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
140 if (TTSD_ERROR_NONE != ret) {
141 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
143 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
144 ttsd_server_stop(uid);
146 int pid = ttsd_data_get_pid(uid);
148 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
150 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
153 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
158 ttsd_data_destroy_speak_data(speak_data);
160 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
166 * TTS Server Callback Functions
168 int ttsd_send_error(ttse_error_e error, const char* msg)
170 unsigned int uid = g_utt.uid;
171 int uttid = g_utt.uttid;
172 int tmp_pid = ttsd_data_get_pid(uid);
174 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));
176 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
178 if (0 != ttsd_player_clear(uid))
179 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
181 if (0 != ttsd_data_clear_data(uid))
182 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
185 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
186 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
189 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
190 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
193 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
194 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
199 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)
201 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
203 unsigned int uid = g_utt.uid;
204 int uttid = g_utt.uttid;
206 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
207 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
208 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
209 SLOG(LOG_DEBUG, tts_tag(), "@@@");
210 return TTSD_ERROR_INVALID_PARAMETER;
213 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
214 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
215 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
216 return TTSD_ERROR_INVALID_PARAMETER;
219 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
220 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
221 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
222 SLOG(LOG_DEBUG, tts_tag(), "@@@");
223 return TTSD_ERROR_INVALID_PARAMETER;
226 /* check the current state */
227 app_tts_state_e state;
228 if (0 > ttsd_data_get_client_state(uid, &state)) {
229 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
230 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
231 return TTSD_ERROR_INVALID_PARAMETER;
234 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
235 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
236 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
237 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
238 return TTSD_ERROR_NONE;
241 /* Synthesis is success */
242 if (TTSE_RESULT_EVENT_START == event) {
243 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
244 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
245 uid, uttid, data, data_size, audio_type, rate);
247 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
248 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
249 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
250 double d_latency = (double)latency;
251 g_avg_latency += d_latency;
252 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
253 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
255 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);
256 } else if (TTSE_RESULT_EVENT_FINISH == event) {
257 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
258 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
259 uid, uttid, data, data_size, audio_type, rate);
261 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
265 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
266 if (NULL == sound_data) {
267 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
268 return TTSD_ERROR_OUT_OF_MEMORY;
271 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
272 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
273 ttsd_data_destroy_sound_data(sound_data);
275 return TTSD_ERROR_OPERATION_FAILED;
278 if (event == TTSE_RESULT_EVENT_FINISH) {
279 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
282 return TTSD_ERROR_NONE;
285 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
287 /* clear client data */
288 ttsd_data_clear_data(uid);
290 if (APP_STATE_READY != state) {
291 ttsd_data_set_client_state(uid, APP_STATE_READY);
294 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
296 ttsd_data_delete_client(uid);
303 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
306 case TTS_CONFIG_TYPE_ENGINE:
308 /* TODO - Determine the policy when engine process get engine changed cb */
309 if (NULL == str_param) {
310 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
315 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
316 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
317 ret = ttsd_engine_agent_unload_current_engine();
319 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
322 ret = ttsd_engine_agent_load_current_engine();
324 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
329 /* stop all player */
330 ttsd_player_all_stop();
332 /* send interrupt message to all clients */
333 ttsd_data_foreach_clients(__get_client_cb, NULL);
335 ttsd_engine_cancel_synthesis();
340 case TTS_CONFIG_TYPE_VOICE:
342 if (NULL == str_param) {
343 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
347 char* out_lang = NULL;
351 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
352 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
353 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
355 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
357 /* Current language is not available */
358 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
360 if (NULL != out_lang) {
367 case TTS_CONFIG_TYPE_SPEED:
369 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
370 /* set default speed */
372 ret = ttsd_engine_agent_set_default_speed(int_param);
374 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
380 case TTS_CONFIG_TYPE_PITCH:
382 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
383 /* set default speed */
385 ret = ttsd_engine_agent_set_default_pitch(int_param);
387 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
393 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
395 if (0.0 <= double_param && double_param <= 1.0) {
396 /* set default bg volume ratio */
398 ret = ttsd_player_set_background_volume_ratio(double_param);
400 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
410 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
412 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
413 ttsd_server_finalize(uid);
417 static void __terminate_server()
419 if (g_is_terminated) {
420 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
424 g_is_terminated = true;
426 ttsd_ipc_close_connection();
427 ttsd_network_finalize();
431 static Eina_Bool __quit_ecore_loop(void *data)
433 __terminate_server();
434 ecore_main_loop_quit();
439 Eina_Bool ttsd_terminate_daemon(void *data)
441 ttsd_data_foreach_clients(__terminate_client, NULL);
443 if (g_quit_loop_timer) {
444 ecore_timer_del(g_quit_loop_timer);
445 g_quit_loop_timer = NULL;
446 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
449 __terminate_server();
451 g_terminate_timer = NULL;
455 void __screen_reader_changed_cb(bool value)
457 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
458 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
459 if (g_terminate_timer) {
460 ecore_timer_del(g_terminate_timer);
461 g_terminate_timer = NULL;
463 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
466 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
471 int ttsd_send_all_stop()
473 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
475 /* stop all player */
476 ttsd_player_all_stop();
478 /* send interrupt message to all clients */
479 ttsd_data_foreach_clients(__get_client_cb, NULL);
481 return TTSD_ERROR_NONE;
487 int ttsd_initialize(ttse_request_callback_s *callback)
489 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
490 g_is_terminated = false;
492 if (ttsd_config_initialize(__config_changed_cb)) {
493 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
497 if (ttsd_player_init()) {
498 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
499 return TTSD_ERROR_OPERATION_FAILED;
503 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
505 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
506 int ret = ttsd_player_set_background_volume_ratio(ratio);
508 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
510 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
513 /* Engine Agent initialize */
514 if (0 != ttsd_engine_agent_init()) {
515 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
516 return TTSD_ERROR_OPERATION_FAILED;
519 /* set current engine */
520 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
521 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
522 // g_is_engine = false;
524 // g_is_engine = true;
526 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
527 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
528 return TTSD_ERROR_OPERATION_FAILED;
531 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
533 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
534 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
537 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
538 if (NULL == g_check_client_timer) {
539 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
542 return TTSD_ERROR_NONE;
547 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
550 if (0 < g_list_length(g_proc_list)) {
551 iter = g_list_first(g_proc_list);
552 while (NULL != iter) {
553 g_proc_list = g_list_remove_link(g_proc_list, iter);
555 iter = g_list_first(g_proc_list);
559 ttsd_config_finalize();
561 ttsd_player_release();
563 ttsd_engine_agent_release();
567 ecore_timer_del(g_wait_timer);
569 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
572 if (g_terminate_timer) {
573 ecore_timer_del(g_terminate_timer);
574 g_terminate_timer = NULL;
575 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
578 if (g_check_client_timer) {
579 ecore_timer_del(g_check_client_timer);
580 g_check_client_timer = NULL;
581 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
584 if (g_quit_loop_timer) {
585 ecore_timer_del(g_quit_loop_timer);
586 g_quit_loop_timer = NULL;
587 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
590 return TTSD_ERROR_NONE;
595 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
597 ttsd_terminate_daemon(NULL);
599 return TTSD_ERROR_NONE;
603 * TTS Server Functions for Client
606 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
608 if (-1 != ttsd_data_is_client(uid))
609 *is_initialized = true;
611 *is_initialized = false;
613 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
614 return TTSD_ERROR_NONE;
617 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
619 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
621 if (-1 != ttsd_data_is_client(uid)) {
622 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
623 return TTSD_ERROR_NONE;
626 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
627 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
628 return TTSD_ERROR_OPERATION_FAILED;
631 if (true == *credential_needed) {
633 if (0 != app_manager_get_app_id(pid, &appid)) {
634 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
636 bool is_agreed = false;
637 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
638 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
641 return TTSD_ERROR_OPERATION_FAILED;
646 if (false == is_agreed) {
647 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
648 return TTSD_ERROR_PERMISSION_DENIED;
652 if (0 != ttsd_data_new_client(pid, uid)) {
653 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
654 return TTSD_ERROR_OPERATION_FAILED;
657 if (0 != ttsd_data_set_ipc_method(uid, method)) {
658 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
659 return TTSD_ERROR_OPERATION_FAILED;
662 if (0 != ttsd_player_create_instance(uid)) {
663 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
664 return TTSD_ERROR_OPERATION_FAILED;
668 g_min_latency = 10000.0;
673 return TTSD_ERROR_NONE;
676 static void __read_proc()
679 struct dirent *dirp = NULL;
683 if (0 < g_list_length(g_proc_list)) {
684 iter = g_list_first(g_proc_list);
685 while (NULL != iter) {
686 g_proc_list = g_list_remove_link(g_proc_list, iter);
688 iter = g_list_first(g_proc_list);
692 dp = opendir("/proc");
694 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
700 tmp = atoi(dirp->d_name);
701 if (0 >= tmp) continue;
702 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
704 } while (NULL != dirp);
710 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
716 for (i = 0; i < g_list_length(g_proc_list); i++) {
717 iter = g_list_nth(g_proc_list, i);
719 if (pid == GPOINTER_TO_INT(iter->data)) {
720 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
727 if (false == exist) {
728 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
729 ttsd_server_finalize(uid);
734 char appid[1024] = {0, };
735 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
736 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
739 if (0 < strlen(appid)) {
740 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
742 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
745 result = ttsdc_send_hello(pid, uid);
748 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
749 ttsd_server_finalize(uid);
750 } else if (-1 == result) {
751 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
759 Eina_Bool ttsd_cleanup_client(void *data)
761 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
764 if (0 < ttsd_data_get_client_count()) {
765 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
767 if (NULL == g_quit_loop_timer) {
768 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
769 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
771 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
772 g_check_client_timer = NULL;
776 SLOG(LOG_DEBUG, tts_tag(), "@@@");
781 void __used_voice_cb(const char* lang, int type)
783 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
784 if (0 != ttsd_engine_unload_voice(lang, type)) {
785 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
789 int ttsd_server_finalize(unsigned int uid)
791 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
792 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);
794 app_tts_state_e state;
795 if (0 > ttsd_data_get_client_state(uid, &state)) {
796 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
799 ttsd_server_stop(uid);
800 ttsd_player_clear(uid);
802 ttsd_player_destroy_instance(uid);
804 /* Need to unload voice when used voice is unregistered */
805 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
806 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
807 return TTSD_ERROR_OPERATION_FAILED;
810 ttsd_data_delete_client(uid);
812 /* unload engine, if ref count of client is 0 */
813 if (0 == ttsd_data_get_client_count()) {
814 if (NULL == g_quit_loop_timer) {
815 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
816 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
818 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
822 return TTSD_ERROR_NONE;
825 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
827 app_tts_state_e state;
828 if (0 > ttsd_data_get_client_state(uid, &state)) {
829 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
830 return TTSD_ERROR_INVALID_PARAMETER;
833 if (APP_STATE_READY != state) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
835 return TTSD_ERROR_INVALID_STATE;
838 int ret = ttsd_data_set_mode(uid, mode);
840 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
842 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
848 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)
850 app_tts_state_e state;
851 if (0 > ttsd_data_get_client_state(uid, &state)) {
852 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
853 return TTSD_ERROR_INVALID_PARAMETER;
856 /* check valid voice */
857 char* temp_lang = NULL;
859 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
860 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
861 if (NULL != temp_lang) {
865 return TTSD_ERROR_INVALID_VOICE;
868 if (NULL == temp_lang) {
869 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
870 return TTSD_ERROR_INVALID_VOICE;
873 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
874 if (NULL == speak_data) {
875 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
876 if (NULL != temp_lang) {
881 return TTSD_ERROR_OPERATION_FAILED;
884 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);
886 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
888 ret = ttsd_data_add_speak_data(uid, speak_data);
890 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
891 if (NULL != temp_lang) {
896 ttsd_data_destroy_speak_data(speak_data);
902 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
903 /* Request load voice */
904 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
905 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
906 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
910 if (NULL != temp_lang) {
915 if (APP_STATE_PLAYING == state) {
916 /* check if engine use network */
917 if (ttsd_engine_agent_need_network()) {
918 if (false == ttsd_network_is_connected()) {
919 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
920 return TTSD_ERROR_OPERATION_FAILED;
924 ttsd_data_set_credential(uid, credential);
926 /* Check whether tts-engine is running or not */
927 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
928 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
929 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
930 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
936 return TTSD_ERROR_NONE;
939 static int __pause_client(unsigned int uid)
941 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
942 if (TTSD_ERROR_NONE != ret) {
943 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
944 return TTSD_ERROR_OPERATION_FAILED;
947 ret = ttsd_player_pause(uid);
948 if (TTSD_ERROR_NONE != ret) {
949 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
950 return TTSD_ERROR_OPERATION_FAILED;
953 return TTSD_ERROR_NONE;
956 void __send_interrupt_client(unsigned int uid)
958 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
959 int pid = ttsd_data_get_pid(uid);
961 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
965 app_tts_state_e state = APP_STATE_CREATED;
967 if (0 != ttsd_data_get_client_state(uid, &state)) {
968 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
972 ttsdc_ipc_send_set_state_message(pid, uid, state);
973 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
977 int ttsd_server_play(unsigned int uid, const char* credential)
979 app_tts_state_e state;
980 if (0 > ttsd_data_get_client_state(uid, &state)) {
981 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
982 return TTSD_ERROR_INVALID_PARAMETER;
985 if (APP_STATE_PLAYING == state) {
986 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
987 return TTSD_ERROR_NONE;
990 /* check if engine use network */
991 if (ttsd_engine_agent_need_network()) {
992 if (false == ttsd_network_is_connected()) {
993 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
994 return TTSD_ERROR_OUT_OF_NETWORK;
998 /* check the current playback focus */
999 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1000 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1001 ttsd_data_clear_data(uid);
1002 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1005 unsigned int current_uid = ttsd_data_get_current_playing();
1006 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1008 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1009 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1010 /* Send interrupt message */
1011 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1014 if (0 != ttsd_server_stop(current_uid)) {
1015 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1018 /* Default mode policy of interrupt is "Pause" */
1020 /* Send interrupt message */
1021 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1024 if (0 != __pause_client(current_uid)) {
1025 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1029 __send_interrupt_client(current_uid);
1032 /* Change current play */
1033 if (0 != ttsd_player_wait_to_play(uid)) {
1034 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1037 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1038 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1039 return TTSD_ERROR_OPERATION_FAILED;
1042 int ret = TTSD_ERROR_NONE;
1043 if (APP_STATE_PAUSED == state) {
1044 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1045 g_is_paused = false;
1046 ret = ttsd_player_resume(uid);
1048 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1049 ret = ttsd_player_play(uid);
1052 if (TTSD_ERROR_NONE != ret) {
1053 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1054 ttsd_data_set_client_state(uid, state);
1055 return TTSD_ERROR_OPERATION_FAILED;
1058 ttsd_data_set_credential(uid, credential);
1060 /* Check whether tts-engine is running or not */
1061 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1062 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1063 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1064 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1065 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1070 return TTSD_ERROR_NONE;
1073 int ttsd_server_stop(unsigned int uid)
1075 app_tts_state_e state;
1076 if (0 > ttsd_data_get_client_state(uid, &state)) {
1077 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1078 return TTSD_ERROR_INVALID_PARAMETER;
1081 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1083 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1084 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1085 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1086 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1087 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1089 int ret = ttsd_engine_cancel_synthesis();
1090 if (TTSD_ERROR_NONE != ret)
1091 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1093 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1096 ttsd_data_set_client_state(uid, APP_STATE_READY);
1097 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1098 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1102 /* Reset all data */
1103 ttsd_data_clear_data(uid);
1104 g_is_paused = false;
1106 return TTSD_ERROR_NONE;
1109 int ttsd_server_pause(unsigned int uid, int* utt_id)
1111 app_tts_state_e state;
1112 if (0 > ttsd_data_get_client_state(uid, &state)) {
1113 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1114 return TTSD_ERROR_INVALID_PARAMETER;
1117 if (APP_STATE_PLAYING != state) {
1118 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1119 return TTSD_ERROR_INVALID_STATE;
1124 *utt_id = g_utt.uttid;
1125 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1127 int ret = __pause_client(uid);
1128 if (TTSD_ERROR_NONE != ret) {
1129 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1130 return TTSD_ERROR_OPERATION_FAILED;
1133 return TTSD_ERROR_NONE;
1136 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1138 app_tts_state_e state;
1139 if (0 > ttsd_data_get_client_state(uid, &state)) {
1140 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1141 return TTSD_ERROR_INVALID_PARAMETER;
1145 int ret = ttsd_engine_get_voice_list(voice_list);
1147 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1151 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1153 return TTSD_ERROR_NONE;
1156 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1158 app_tts_state_e state;
1159 if (0 > ttsd_data_get_client_state(uid, &state)) {
1160 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1161 return TTSD_ERROR_INVALID_PARAMETER;
1164 /* get current voice */
1165 int ret = ttsd_engine_get_default_voice(language, voice_type);
1167 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1171 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1173 return TTSD_ERROR_NONE;
1176 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1178 app_tts_state_e state;
1179 if (0 > ttsd_data_get_client_state(uid, &state)) {
1180 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1181 return TTSD_ERROR_INVALID_PARAMETER;
1184 if (APP_STATE_READY != state) {
1185 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1186 return TTSD_ERROR_INVALID_STATE;
1189 int ret = ttsd_engine_set_private_data(key, data);
1191 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1193 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1199 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1201 app_tts_state_e state;
1202 if (0 > ttsd_data_get_client_state(uid, &state)) {
1203 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1204 return TTSD_ERROR_INVALID_PARAMETER;
1207 if (APP_STATE_READY != state) {
1208 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1209 return TTSD_ERROR_INVALID_STATE;
1212 int ret = ttsd_engine_get_private_data(key, data);
1214 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1216 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1222 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1224 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1227 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1229 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1235 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1237 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1240 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1242 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1248 int ttsd_server_play_pcm(unsigned int uid)
1250 app_tts_state_e state;
1251 if (0 > ttsd_data_get_client_state(uid, &state)) {
1252 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1253 return TTSD_ERROR_INVALID_PARAMETER;
1256 if (APP_STATE_PLAYING == state) {
1257 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1258 return TTSD_ERROR_NONE;
1261 unsigned int current_uid = ttsd_data_get_current_playing();
1262 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1264 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1265 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1266 /* Send interrupt message */
1267 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1270 if (0 != ttsd_server_stop(current_uid)) {
1271 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1274 /* Default mode policy of interrupt is "Pause" */
1276 /* Send interrupt message */
1277 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1280 if (0 != __pause_client(current_uid)) {
1281 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1285 __send_interrupt_client(current_uid);
1288 /* Change current play */
1289 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1290 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1291 return TTSD_ERROR_OPERATION_FAILED;
1294 int ret = TTSD_ERROR_NONE;
1295 if (APP_STATE_PAUSED == state) {
1296 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1297 g_is_paused = false;
1298 ret = ttsd_player_resume(uid);
1300 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1301 ret = ttsd_player_play(uid);
1304 if (TTSD_ERROR_NONE != ret) {
1305 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1306 ttsd_data_set_client_state(uid, state);
1307 return TTSD_ERROR_OPERATION_FAILED;
1310 return TTSD_ERROR_NONE;
1313 int ttsd_server_stop_pcm(unsigned int uid)
1315 app_tts_state_e state;
1316 if (0 > ttsd_data_get_client_state(uid, &state)) {
1317 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1318 return TTSD_ERROR_INVALID_PARAMETER;
1321 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1323 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1324 ttsd_data_set_client_state(uid, APP_STATE_READY);
1327 /* Reset all data */
1328 ttsd_data_clear_data(uid);
1330 ttsd_player_stop(uid);
1333 return TTSD_ERROR_NONE;
1336 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1338 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1342 /* Synthesis is success */
1343 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1344 if (TTSE_RESULT_EVENT_START == event) {
1345 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1346 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1347 uid, uttid, data, data_size, audio_type, rate);
1348 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1349 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1350 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1351 uid, uttid, data, data_size, audio_type, rate);
1353 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1356 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1357 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1358 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1359 return TTSD_ERROR_INVALID_PARAMETER;
1362 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1363 if (NULL == sound_data) {
1364 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1365 return TTSD_ERROR_OUT_OF_MEMORY;
1368 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1369 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1370 ttsd_data_destroy_sound_data(sound_data);
1373 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1376 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1378 return TTSD_ERROR_NONE;