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"
22 #include "ttsd_engine_agent.h"
23 #include "ttsd_main.h"
24 #include "ttsd_player.h"
25 #include "ttsd_state.h"
26 #include "ttsd_server.h"
29 #define CLIENT_CLEAN_UP_TIME 500
31 /* for checking time */
32 #define TIME_DIFF(start, end) \
33 ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
35 static struct timespec g_request_playing, g_start_playing;
36 static double g_avg_latency;
37 static double g_min_latency;
38 static double g_max_latency;
39 static int g_file_num;
49 } state_changed_args_t;
51 /* If current engine exist */
52 //static bool g_is_engine;
54 static Ecore_Timer* g_check_client_timer = NULL;
55 static Ecore_Timer* g_wait_timer = NULL;
56 static Ecore_Timer* g_quit_loop_timer = NULL;
57 static Ecore_Timer* g_notify_state_timer = NULL;
59 static utterance_t g_utt;
61 static GList *g_proc_list = NULL;
63 static bool g_is_terminated = false;
65 static int g_activated_mode = 0;
67 /* Function definitions */
68 static int __stop_and_send_ready_state(unsigned int uid)
70 int ret = ttsd_server_stop(uid);
71 if (TTSD_ERROR_NONE != ret) {
72 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
76 int pid = ttsd_data_get_pid(uid);
78 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
79 return TTSD_ERROR_OPERATION_FAILED;
81 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
87 static void __synthesis(unsigned int uid);
89 static Eina_Bool __wait_synthesis(void *data)
91 /* get current play */
92 unsigned int uid = ttsd_data_get_current_playing();
95 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
96 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
99 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
101 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
102 /* Start next synthesis */
113 static void __synthesis(unsigned int uid)
115 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
117 speak_data_s* speak_data = NULL;
118 int ret = ttsd_data_get_speak_data(uid, &speak_data);
119 if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
120 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
121 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
125 int pid = ttsd_data_get_pid(uid);
127 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
128 ttsd_data_destroy_speak_data(speak_data);
133 char appid[1024] = {0, };
134 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
135 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
138 if (NULL == speak_data->lang || NULL == speak_data->text) {
139 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
140 __stop_and_send_ready_state(uid);
141 ttsd_data_destroy_speak_data(speak_data);
147 g_utt.uttid = speak_data->utt_id;
149 char* credential = ttsd_data_get_credential(uid);
151 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
152 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
153 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
154 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
155 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
156 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
158 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
159 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
160 if (TTSD_ERROR_NONE != ret) {
161 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
163 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
164 __stop_and_send_ready_state(uid);
166 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
169 if (ttsd_data_get_synth_control() == TTSD_SYNTHESIS_CONTROL_DOING && ttsd_state_get_state() == TTSD_STATE_READY) {
170 ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
175 ttsd_data_destroy_speak_data(speak_data);
177 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
183 * TTS Server Callback Functions
185 int ttsd_send_error(ttse_error_e error, const char* msg)
187 unsigned int uid = g_utt.uid;
188 int uttid = g_utt.uttid;
189 int tmp_pid = ttsd_data_get_pid(uid);
191 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));
193 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
195 if (0 != ttsd_data_clear_data(uid))
196 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
199 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
200 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
203 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
204 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
207 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
208 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
213 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)
215 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
217 unsigned int uid = g_utt.uid;
218 int uttid = g_utt.uttid;
220 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
221 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
222 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
223 SLOG(LOG_DEBUG, tts_tag(), "@@@");
224 return TTSD_ERROR_INVALID_PARAMETER;
227 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
228 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
229 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
230 return TTSD_ERROR_INVALID_PARAMETER;
233 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
234 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
235 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
236 SLOG(LOG_DEBUG, tts_tag(), "@@@");
237 return TTSD_ERROR_INVALID_PARAMETER;
240 /* check the current state */
241 app_tts_state_e state = ttsd_data_get_client_state(uid);
242 if (APP_STATE_NONE == state) {
243 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
244 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
245 return TTSD_ERROR_INVALID_PARAMETER;
248 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
249 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
250 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
251 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
252 return TTSD_ERROR_NONE;
255 /* Synthesis is success */
256 if (TTSE_RESULT_EVENT_START == event) {
257 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
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 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
262 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
263 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
264 double d_latency = (double)latency;
265 g_avg_latency += d_latency;
266 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
267 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
269 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);
270 } else if (TTSE_RESULT_EVENT_FINISH == event) {
271 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
272 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
273 uid, uttid, data, data_size, audio_type, rate);
275 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
279 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
280 if (NULL == sound_data) {
281 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
282 return TTSD_ERROR_OUT_OF_MEMORY;
285 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
286 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
287 ttsd_data_destroy_sound_data(sound_data);
289 return TTSD_ERROR_OPERATION_FAILED;
292 if (event == TTSE_RESULT_EVENT_FINISH) {
293 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
296 return TTSD_ERROR_NONE;
299 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
301 if (0 > ttsd_data_is_client(uid)) {
302 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
303 ttsd_data_delete_client(uid);
307 /* clear client data */
308 ttsd_data_clear_data(uid);
310 if (APP_STATE_READY != state) {
311 ttsd_data_set_client_state(uid, APP_STATE_READY);
312 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
318 static void __stop_all_client()
320 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
322 ttsd_engine_cancel_synthesis();
323 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
325 /* stop all player */
326 ttsd_player_all_stop();
328 /* send interrupt message to all clients */
329 ttsd_data_foreach_clients(__get_client_cb, NULL);
332 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
335 case TTS_CONFIG_TYPE_ENGINE:
337 /* TODO - Determine the policy when engine process get engine changed cb */
338 if (NULL == str_param) {
339 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
344 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
345 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
346 ret = ttsd_engine_agent_unload_current_engine();
348 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
351 ret = ttsd_engine_agent_load_current_engine();
353 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
362 case TTS_CONFIG_TYPE_VOICE:
364 if (NULL == str_param) {
365 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
369 char* out_lang = NULL;
373 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
374 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
375 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
377 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
379 /* Current language is not available */
380 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
382 if (NULL != out_lang) {
389 case TTS_CONFIG_TYPE_SPEED:
391 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
392 /* set default speed */
394 ret = ttsd_engine_agent_set_default_speed(int_param);
396 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
402 case TTS_CONFIG_TYPE_PITCH:
404 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
405 /* set default speed */
407 ret = ttsd_engine_agent_set_default_pitch(int_param);
409 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
415 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
417 if (0.0 <= double_param && double_param <= 1.0) {
418 /* set default bg volume ratio */
420 ret = ttsd_player_set_background_volume_ratio(double_param);
422 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
432 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
434 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
435 ttsd_server_finalize(uid);
439 static int __terminate_server()
441 if (g_is_terminated) {
442 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
443 return TTSD_ERROR_NONE;
446 int ret = TTSD_ERROR_NONE;
447 ret = ttsd_ipc_close_connection();
448 if (TTSD_ERROR_NONE != ret) {
449 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
450 return TTSD_ERROR_OPERATION_FAILED;
453 ret = ttsd_finalize();
454 if (TTSD_ERROR_NONE != ret) {
455 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
456 return TTSD_ERROR_OPERATION_FAILED;
459 g_is_terminated = true;
460 return TTSD_ERROR_NONE;
463 static Eina_Bool __quit_ecore_loop(void *data)
465 __terminate_server();
466 ecore_main_loop_quit();
471 void __screen_reader_changed_cb(bool value)
473 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
477 static void __read_proc()
480 struct dirent *dirp = NULL;
484 if (0 < g_list_length(g_proc_list)) {
485 iter = g_list_first(g_proc_list);
486 while (NULL != iter) {
487 g_proc_list = g_list_remove_link(g_proc_list, iter);
489 iter = g_list_first(g_proc_list);
493 dp = opendir("/proc");
495 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
501 tmp = atoi(dirp->d_name);
502 if (0 >= tmp) continue;
503 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
505 } while (NULL != dirp);
511 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
517 for (i = 0; i < g_list_length(g_proc_list); i++) {
518 iter = g_list_nth(g_proc_list, i);
520 if (pid == GPOINTER_TO_INT(iter->data)) {
521 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
528 if (false == exist) {
529 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
530 ttsd_server_finalize(uid);
536 static Eina_Bool __cleanup_client(void *data)
538 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
541 if (0 < ttsd_data_get_client_count()) {
542 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
544 if (NULL == g_quit_loop_timer) {
545 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
546 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
548 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
549 g_check_client_timer = NULL;
553 SLOG(LOG_DEBUG, tts_tag(), "@@@");
558 static bool __notify_state_changed_event(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
560 state_changed_args_t* args = (state_changed_args_t*)user_data;
565 ttsdc_ipc_send_set_service_state_message(pid, uid, args->before, args->current);
569 static Eina_Bool __state_changed_timer_cb(void *data)
572 SLOG(LOG_ERROR, tts_tag(), "[Server] Invalid data passed.");
573 return ECORE_CALLBACK_CANCEL;
576 int ret = ttsd_data_foreach_clients(__notify_state_changed_event, data);
577 if (TTSD_ERROR_NONE != ret) {
578 SLOG(LOG_ERROR, tts_tag(), "[Server] Fail to notify state change.");
582 g_notify_state_timer = NULL;
583 return ECORE_CALLBACK_CANCEL;
586 static void __state_changed_cb(ttsd_state_e before, ttsd_state_e current)
588 state_changed_args_t* args = (state_changed_args_t*)calloc(1, sizeof(state_changed_args_t));
590 args->before = before;
591 args->current = current;
594 SLOG(LOG_INFO, tts_tag(), "[Server] Notify state chanaged from (%d) to (%d).", before, current);
595 g_notify_state_timer = ecore_timer_add(0.0, __state_changed_timer_cb, args);
601 int ttsd_initialize(ttse_request_callback_s *callback)
603 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
604 g_is_terminated = false;
606 if (ttsd_config_initialize(__config_changed_cb)) {
607 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
610 if (TTSD_ERROR_NONE != ttsd_state_initialize(__state_changed_cb)) {
611 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
615 if (ttsd_player_init()) {
616 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
617 return TTSD_ERROR_OPERATION_FAILED;
621 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
623 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
624 int ret = ttsd_player_set_background_volume_ratio(ratio);
626 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
628 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
631 /* Engine Agent initialize */
632 if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
633 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
634 return TTSD_ERROR_OPERATION_FAILED;
637 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
638 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
640 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
641 if (NULL == g_check_client_timer) {
642 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
645 g_activated_mode = 0;
647 return TTSD_ERROR_NONE;
652 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
655 if (0 < g_list_length(g_proc_list)) {
656 iter = g_list_first(g_proc_list);
657 while (NULL != iter) {
658 g_proc_list = g_list_remove_link(g_proc_list, iter);
660 iter = g_list_first(g_proc_list);
665 ecore_timer_del(g_wait_timer);
667 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
670 if (g_check_client_timer) {
671 ecore_timer_del(g_check_client_timer);
672 g_check_client_timer = NULL;
673 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
676 if (g_quit_loop_timer) {
677 ecore_timer_del(g_quit_loop_timer);
678 g_quit_loop_timer = NULL;
679 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
682 ttsd_config_finalize();
683 ttsd_state_finalize();
685 int ret = TTSD_ERROR_NONE;
686 ret = ttsd_player_release();
687 if (TTSD_ERROR_NONE != ret) {
688 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
689 return TTSD_ERROR_OPERATION_FAILED;
692 ret = ttsd_engine_agent_release();
693 if (TTSD_ERROR_NONE != ret) {
694 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
695 return TTSD_ERROR_OPERATION_FAILED;
698 return TTSD_ERROR_NONE;
703 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
704 int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
705 if (TTSD_ERROR_NONE != ret) {
706 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
707 return TTSD_ERROR_OPERATION_FAILED;
710 if (g_quit_loop_timer) {
711 ecore_timer_del(g_quit_loop_timer);
712 g_quit_loop_timer = NULL;
713 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
716 return __terminate_server();
720 * TTS Server Functions for Client
723 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
725 if (-1 != ttsd_data_is_client(uid))
726 *is_initialized = true;
728 *is_initialized = false;
730 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
731 return TTSD_ERROR_NONE;
734 static void __set_and_notify_activated_mode()
736 int activated_mode = ttsd_data_get_activated_mode();
737 if (g_activated_mode == activated_mode) {
741 SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
743 g_activated_mode = activated_mode;
744 int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
745 if (TTSD_ERROR_NONE != ret) {
746 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
750 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
752 if (false == credential_needed) {
753 return TTSD_ERROR_NONE;
757 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
758 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
761 bool is_agreed = false;
762 int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
766 if (TTSD_ERROR_NONE != ret) {
767 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
768 return TTSD_ERROR_OPERATION_FAILED;
771 if (false == is_agreed) {
772 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
773 return TTSD_ERROR_PERMISSION_DENIED;
776 return TTSD_ERROR_NONE;
779 int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_method_e method, bool* credential_needed)
781 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
783 if (-1 != ttsd_data_is_client(uid)) {
784 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
785 return TTSD_ERROR_NONE;
788 if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, method)) {
789 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
790 return TTSD_ERROR_OPERATION_FAILED;
793 __set_and_notify_activated_mode();
795 if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
796 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
797 ttsd_data_delete_client(uid);
798 return TTSD_ERROR_OPERATION_FAILED;
801 bool is_needed = false;
802 if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
803 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
804 ttsd_data_delete_client(uid);
805 return TTSD_ERROR_OPERATION_FAILED;
808 int ret = __check_app_agreed(pid, uid, is_needed);
809 if (TTSD_ERROR_NONE != ret) {
810 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
811 ttsd_data_delete_client(uid);
815 *credential_needed = is_needed;
817 return TTSD_ERROR_NONE;
820 void __used_voice_cb(const char* lang, int type)
822 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
823 if (0 != ttsd_engine_unload_voice(lang, type)) {
824 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
828 int ttsd_server_finalize(unsigned int uid)
830 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
831 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);
833 if (0 > ttsd_data_is_client(uid)) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
837 int ret = ttsd_server_stop(uid);
838 if (TTSD_ERROR_NONE != ret) {
839 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
842 /* Need to unload voice when used voice is unregistered */
843 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
844 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
845 return TTSD_ERROR_OPERATION_FAILED;
848 ttsd_data_delete_client(uid);
849 __set_and_notify_activated_mode();
851 /* unload engine, if ref count of client is 0 */
852 if (0 == ttsd_data_get_client_count()) {
853 if (NULL == g_quit_loop_timer) {
854 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
855 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
857 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
861 return TTSD_ERROR_NONE;
864 static bool __is_connected_to_network()
867 int network_status = 0;
868 int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
870 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
874 if (network_status == VCONFKEY_NETWORK_OFF) {
875 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
879 SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
884 int ttsd_server_add_text(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
886 app_tts_state_e state = ttsd_data_get_client_state(uid);
887 if (APP_STATE_NONE == state) {
888 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
889 return TTSD_ERROR_INVALID_PARAMETER;
892 /* check valid voice */
893 char* temp_lang = NULL;
895 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
896 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
897 if (NULL != temp_lang) {
901 return TTSD_ERROR_INVALID_VOICE;
904 if (NULL == temp_lang) {
905 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
906 return TTSD_ERROR_INVALID_VOICE;
909 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
910 if (NULL == speak_data) {
911 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
912 if (NULL != temp_lang) {
917 return TTSD_ERROR_OPERATION_FAILED;
920 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);
922 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
924 ret = ttsd_data_add_speak_data(uid, speak_data);
926 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
927 if (NULL != temp_lang) {
932 ttsd_data_destroy_speak_data(speak_data);
938 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
939 /* Request load voice */
940 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
941 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
942 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
946 if (NULL != temp_lang) {
951 if (APP_STATE_PLAYING == state) {
952 /* check if engine use network */
953 if (ttsd_engine_agent_need_network()) {
954 if (false == __is_connected_to_network()) {
955 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
956 return TTSD_ERROR_OPERATION_FAILED;
960 ttsd_data_set_credential(uid, credential);
962 /* Check whether tts-engine is running or not */
963 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
964 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
965 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
966 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
972 return TTSD_ERROR_NONE;
975 static int __interrupt_player_by_policy(unsigned int request_app_uid)
977 unsigned int playing_app_uid = ttsd_data_get_current_playing();
978 if (0 > ttsd_data_is_client(playing_app_uid)) {
979 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
980 return TTSD_ERROR_NONE;
983 if (request_app_uid == playing_app_uid) {
984 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
985 return TTSD_ERROR_NONE;
988 ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
989 ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
990 switch (playing_app_mode) {
991 case TTSD_MODE_DEFAULT:
992 case TTSD_MODE_NOTIFICATION:
993 case TTSD_MODE_SCREEN_READER:
994 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
995 __stop_and_send_ready_state(playing_app_uid);
998 case TTSD_MODE_INTERRUPT:
999 if (TTSD_MODE_INTERRUPT != request_app_mode) {
1000 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
1001 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1004 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
1005 __stop_and_send_ready_state(playing_app_uid);
1009 return TTSD_ERROR_NONE;
1012 int ttsd_server_play(unsigned int uid, const char* credential)
1014 app_tts_state_e state = ttsd_data_get_client_state(uid);
1015 if (APP_STATE_NONE == state) {
1016 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1017 return TTSD_ERROR_INVALID_PARAMETER;
1020 if (APP_STATE_PLAYING == state) {
1021 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1022 return TTSD_ERROR_NONE;
1025 /* check if engine use network */
1026 if (ttsd_engine_agent_need_network()) {
1027 if (false == __is_connected_to_network()) {
1028 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1029 return TTSD_ERROR_OUT_OF_NETWORK;
1033 int ret = __interrupt_player_by_policy(uid);
1034 if (TTSD_ERROR_NONE != ret) {
1035 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1039 /* Change current play */
1040 if (0 != ttsd_player_wait_to_play(uid)) {
1041 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1044 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1045 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1046 return TTSD_ERROR_OPERATION_FAILED;
1049 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1050 ret = ttsd_player_play(uid);
1051 if (TTSD_ERROR_NONE != ret) {
1052 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1053 ttsd_data_set_client_state(uid, state);
1054 return TTSD_ERROR_OPERATION_FAILED;
1057 ttsd_data_set_credential(uid, credential);
1059 /* Check whether tts-engine is running or not */
1060 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1061 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1062 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1063 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1064 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1068 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1070 return TTSD_ERROR_NONE;
1073 static void __stop_engine_synthesis(unsigned int uid)
1075 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1076 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1078 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1079 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1081 int ret = ttsd_engine_cancel_synthesis();
1082 if (TTSD_ERROR_NONE != ret)
1083 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1086 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1089 int ttsd_server_stop(unsigned int uid)
1091 app_tts_state_e state = ttsd_data_get_client_state(uid);
1092 if (APP_STATE_NONE == state) {
1093 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1094 return TTSD_ERROR_INVALID_PARAMETER;
1097 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1099 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1100 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1101 __stop_engine_synthesis(uid);
1105 ttsd_data_set_client_state(uid, APP_STATE_READY);
1106 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1107 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1111 /* Reset all data */
1112 ttsd_data_clear_data(uid);
1114 return TTSD_ERROR_NONE;
1117 int ttsd_server_pause(unsigned int uid)
1119 app_tts_state_e state = ttsd_data_get_client_state(uid);
1120 if (APP_STATE_NONE == state) {
1121 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1122 return TTSD_ERROR_INVALID_PARAMETER;
1125 if (APP_STATE_PLAYING != state) {
1126 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1127 return TTSD_ERROR_INVALID_STATE;
1130 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1131 if (TTSD_ERROR_NONE != ret) {
1132 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1133 return TTSD_ERROR_OPERATION_FAILED;
1136 ret = ttsd_player_pause(uid);
1137 if (TTSD_ERROR_NONE != ret) {
1138 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1139 return TTSD_ERROR_OPERATION_FAILED;
1142 return TTSD_ERROR_NONE;
1145 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1147 if (0 > ttsd_data_is_client(uid)) {
1148 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1149 return TTSD_ERROR_INVALID_PARAMETER;
1153 int ret = ttsd_engine_get_voice_list(voice_list);
1155 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1159 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1161 return TTSD_ERROR_NONE;
1164 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1166 if (0 > ttsd_data_is_client(uid)) {
1167 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1168 return TTSD_ERROR_INVALID_PARAMETER;
1171 /* get current voice */
1172 int ret = ttsd_engine_get_default_voice(language, voice_type);
1174 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1178 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1180 return TTSD_ERROR_NONE;
1183 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1185 app_tts_state_e state = ttsd_data_get_client_state(uid);
1186 if (APP_STATE_NONE == state) {
1187 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1188 return TTSD_ERROR_INVALID_PARAMETER;
1191 if (APP_STATE_READY != state) {
1192 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1193 return TTSD_ERROR_INVALID_STATE;
1196 int ret = ttsd_engine_set_private_data(key, data);
1198 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1200 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1206 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1208 app_tts_state_e state = ttsd_data_get_client_state(uid);
1209 if (APP_STATE_NONE == state) {
1210 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1211 return TTSD_ERROR_INVALID_PARAMETER;
1214 if (APP_STATE_READY != state) {
1215 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1216 return TTSD_ERROR_INVALID_STATE;
1219 char* temp_data = NULL;
1220 int ret = ttsd_engine_get_private_data(key, &temp_data);
1222 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1224 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1227 if (NULL == temp_data) {
1228 temp_data = strdup("NULL");
1235 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1237 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1240 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1242 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1248 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1250 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1253 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1255 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1261 int ttsd_get_activated_mode(int* activated_mode)
1263 if (NULL == activated_mode) {
1264 return TTSD_ERROR_INVALID_PARAMETER;
1267 *activated_mode = ttsd_data_get_activated_mode();
1268 return TTSD_ERROR_NONE;
1271 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1273 int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1274 if (TTSD_ERROR_NONE != ret) {
1275 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1281 int ttsd_server_play_pcm(unsigned int uid)
1283 app_tts_state_e state = ttsd_data_get_client_state(uid);
1284 if (APP_STATE_NONE == state) {
1285 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1286 return TTSD_ERROR_INVALID_PARAMETER;
1289 if (APP_STATE_PLAYING == state) {
1290 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1291 return TTSD_ERROR_NONE;
1294 int ret = __interrupt_player_by_policy(uid);
1295 if (TTSD_ERROR_NONE != ret) {
1296 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1300 /* Change current play */
1301 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1302 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1303 return TTSD_ERROR_OPERATION_FAILED;
1306 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1307 ret = ttsd_player_play(uid);
1308 if (TTSD_ERROR_NONE != ret) {
1309 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1310 ttsd_data_set_client_state(uid, state);
1311 return TTSD_ERROR_OPERATION_FAILED;
1314 ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1316 return TTSD_ERROR_NONE;
1319 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1321 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1325 /* Synthesis is success */
1326 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1327 if (TTSE_RESULT_EVENT_START == event) {
1328 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1329 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1330 uid, uttid, data, data_size, audio_type, rate);
1331 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1332 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1333 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1334 uid, uttid, data, data_size, audio_type, rate);
1336 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1339 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1340 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1341 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1342 return TTSD_ERROR_INVALID_PARAMETER;
1345 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1346 if (NULL == sound_data) {
1347 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1348 return TTSD_ERROR_OUT_OF_MEMORY;
1351 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1352 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1353 ttsd_data_destroy_sound_data(sound_data);
1356 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1359 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1361 return TTSD_ERROR_NONE;
1364 int ttsd_server_get_service_state(unsigned int uid, int* service_state)
1366 if (0 > ttsd_data_is_client(uid)) {
1367 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_service_state : uid is not valid");
1368 return TTSD_ERROR_INVALID_PARAMETER;
1371 *service_state = (int)ttsd_state_get_state();
1372 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get current service state. service state(%d) ", *service_state);
1374 return TTSD_ERROR_NONE;