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 int __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 int __synthesis(unsigned int uid)
94 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
96 speak_data_s* speak_data = NULL;
97 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
98 if (NULL == speak_data) {
99 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
103 int pid = ttsd_data_get_pid(uid);
105 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
106 ttsd_data_destroy_speak_data(speak_data);
111 char appid[1024] = {0, };
112 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
113 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
116 if (NULL == speak_data->lang || NULL == speak_data->text) {
117 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
118 ttsd_server_stop(uid);
120 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
122 ttsd_data_destroy_speak_data(speak_data);
129 g_utt.uttid = speak_data->utt_id;
131 char* credential = ttsd_data_get_credential(uid);
133 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
134 SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
135 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
136 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
137 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
138 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
141 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
142 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
144 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
146 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
148 ttsd_server_stop(uid);
150 int pid = ttsd_data_get_pid(uid);
152 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
154 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
157 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, NULL);
162 ttsd_data_destroy_speak_data(speak_data);
165 ttsd_data_destroy_speak_data(speak_data);
169 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
175 * TTS Server Callback Functions
177 int ttsd_send_error(ttse_error_e error, const char* msg)
179 unsigned int uid = g_utt.uid;
180 int uttid = g_utt.uttid;
181 int tmp_pid = ttsd_data_get_pid(uid);
183 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));
185 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
187 if (0 != ttsd_player_clear(uid))
188 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
190 if (0 != ttsd_data_clear_data(uid))
191 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
194 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
195 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
198 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
199 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
202 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
203 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
208 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)
210 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
212 unsigned int uid = g_utt.uid;
213 int uttid = g_utt.uttid;
215 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
216 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
217 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
218 SLOG(LOG_DEBUG, tts_tag(), "@@@");
219 return TTSD_ERROR_INVALID_PARAMETER;
222 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
223 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
224 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
225 return TTSD_ERROR_INVALID_PARAMETER;
228 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
229 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
230 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
231 SLOG(LOG_DEBUG, tts_tag(), "@@@");
232 return TTSD_ERROR_INVALID_PARAMETER;
235 /* check the current state */
236 app_tts_state_e state;
237 if (0 > ttsd_data_get_client_state(uid, &state)) {
238 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
239 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
240 return TTSD_ERROR_INVALID_PARAMETER;
243 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
244 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
245 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
246 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
247 return TTSD_ERROR_NONE;
250 /* Synthesis is success */
251 if (TTSE_RESULT_EVENT_START == event) {
252 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
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 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
257 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
258 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
259 double d_latency = (double)latency;
260 g_avg_latency += d_latency;
261 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
262 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
264 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);
265 } else if (TTSE_RESULT_EVENT_FINISH == event) {
266 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
267 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
268 uid, uttid, data, data_size, audio_type, rate);
270 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
274 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
275 if (NULL == sound_data) {
276 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
277 return TTSD_ERROR_OUT_OF_MEMORY;
280 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
281 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
282 ttsd_data_destroy_sound_data(sound_data);
284 return TTSD_ERROR_OPERATION_FAILED;
287 if (event == TTSE_RESULT_EVENT_FINISH) {
288 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
291 return TTSD_ERROR_NONE;
294 bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
296 /* clear client data */
297 ttsd_data_clear_data(uid);
299 if (APP_STATE_READY != state) {
300 ttsd_data_set_client_state(uid, APP_STATE_READY);
303 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
305 ttsd_data_delete_client(uid);
312 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
315 case TTS_CONFIG_TYPE_ENGINE:
317 /* TODO - Determine the policy when engine process get engine changed cb */
318 if (NULL == str_param) {
319 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
324 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
325 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
326 ret = ttsd_engine_agent_unload_current_engine();
328 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
331 ret = ttsd_engine_agent_load_current_engine();
333 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
338 /* stop all player */
339 ttsd_player_all_stop();
341 /* send interrupt message to all clients */
342 ttsd_data_foreach_clients(__get_client_cb, NULL);
344 ttsd_engine_cancel_synthesis();
349 case TTS_CONFIG_TYPE_VOICE:
351 if (NULL == str_param) {
352 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
356 char* out_lang = NULL;
360 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
361 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
362 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
364 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
366 /* Current language is not available */
367 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
369 if (NULL != out_lang) {
376 case TTS_CONFIG_TYPE_SPEED:
378 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
379 /* set default speed */
381 ret = ttsd_engine_agent_set_default_speed(int_param);
383 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
389 case TTS_CONFIG_TYPE_PITCH:
391 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
392 /* set default speed */
394 ret = ttsd_engine_agent_set_default_pitch(int_param);
396 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
402 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
404 if (0.0 <= double_param && double_param <= 1.0) {
405 /* set default bg volume ratio */
407 ret = ttsd_player_set_background_volume_ratio(double_param);
409 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
419 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
421 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
422 ttsd_server_finalize(uid);
426 static void __terminate_server()
428 if (g_is_terminated) {
429 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
433 g_is_terminated = true;
435 ttsd_ipc_close_connection();
436 ttsd_network_finalize();
440 static Eina_Bool __quit_ecore_loop(void *data)
442 __terminate_server();
443 ecore_main_loop_quit();
448 Eina_Bool ttsd_terminate_daemon(void *data)
450 ttsd_data_foreach_clients(__terminate_client, NULL);
452 if (g_quit_loop_timer) {
453 ecore_timer_del(g_quit_loop_timer);
454 g_quit_loop_timer = NULL;
455 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
458 __terminate_server();
460 g_terminate_timer = NULL;
464 void __screen_reader_changed_cb(bool value)
466 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
467 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
468 if (g_terminate_timer) {
469 ecore_timer_del(g_terminate_timer);
470 g_terminate_timer = NULL;
472 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
475 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
480 int ttsd_send_all_stop()
482 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
484 /* stop all player */
485 ttsd_player_all_stop();
487 /* send interrupt message to all clients */
488 ttsd_data_foreach_clients(__get_client_cb, NULL);
490 return TTSD_ERROR_NONE;
496 int ttsd_initialize(ttse_request_callback_s *callback)
498 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
499 g_is_terminated = false;
501 if (ttsd_config_initialize(__config_changed_cb)) {
502 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
506 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
508 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
509 int ret = ttsd_player_set_background_volume_ratio(ratio);
511 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
514 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
517 if (ttsd_player_init()) {
518 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
519 return TTSD_ERROR_OPERATION_FAILED;
522 /* Engine Agent initialize */
523 if (0 != ttsd_engine_agent_init()) {
524 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
525 return TTSD_ERROR_OPERATION_FAILED;
528 /* set current engine */
529 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
530 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
531 // g_is_engine = false;
533 // g_is_engine = true;
535 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
536 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
537 return TTSD_ERROR_OPERATION_FAILED;
540 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
542 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
543 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
546 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
547 if (NULL == g_check_client_timer) {
548 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
551 return TTSD_ERROR_NONE;
556 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
559 if (0 < g_list_length(g_proc_list)) {
560 iter = g_list_first(g_proc_list);
561 while (NULL != iter) {
562 g_proc_list = g_list_remove_link(g_proc_list, iter);
564 iter = g_list_first(g_proc_list);
568 ttsd_config_finalize();
570 ttsd_player_release();
572 ttsd_engine_agent_release();
576 ecore_timer_del(g_wait_timer);
578 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
581 if (g_terminate_timer) {
582 ecore_timer_del(g_terminate_timer);
583 g_terminate_timer = NULL;
584 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
587 if (g_check_client_timer) {
588 ecore_timer_del(g_check_client_timer);
589 g_check_client_timer = NULL;
590 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
593 if (g_quit_loop_timer) {
594 ecore_timer_del(g_quit_loop_timer);
595 g_quit_loop_timer = NULL;
596 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
599 return TTSD_ERROR_NONE;
604 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
606 ttsd_terminate_daemon(NULL);
608 return TTSD_ERROR_NONE;
612 * TTS Server Functions for Client
615 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
617 if (-1 != ttsd_data_is_client(uid))
618 *is_initialized = true;
620 *is_initialized = false;
622 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
623 return TTSD_ERROR_NONE;
626 int ttsd_server_initialize(int pid, unsigned int uid, tts_ipc_method_e method, bool* credential_needed)
628 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
630 if (-1 != ttsd_data_is_client(uid)) {
631 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
632 return TTSD_ERROR_NONE;
635 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
636 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
637 return TTSD_ERROR_OPERATION_FAILED;
640 if (true == *credential_needed) {
642 if (0 != app_manager_get_app_id(pid, &appid)) {
643 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
645 bool is_agreed = false;
646 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
647 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
650 return TTSD_ERROR_OPERATION_FAILED;
655 if (false == is_agreed) {
656 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
657 return TTSD_ERROR_PERMISSION_DENIED;
661 if (0 != ttsd_data_new_client(pid, uid)) {
662 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
663 return TTSD_ERROR_OPERATION_FAILED;
666 if (0 != ttsd_data_set_ipc_method(uid, method)) {
667 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
668 return TTSD_ERROR_OPERATION_FAILED;
671 if (0 != ttsd_player_create_instance(uid)) {
672 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
673 return TTSD_ERROR_OPERATION_FAILED;
677 g_min_latency = 10000.0;
682 return TTSD_ERROR_NONE;
685 static void __read_proc()
688 struct dirent *dirp = NULL;
692 if (0 < g_list_length(g_proc_list)) {
693 iter = g_list_first(g_proc_list);
694 while (NULL != iter) {
695 g_proc_list = g_list_remove_link(g_proc_list, iter);
697 iter = g_list_first(g_proc_list);
701 dp = opendir("/proc");
703 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
709 tmp = atoi(dirp->d_name);
710 if (0 >= tmp) continue;
711 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
713 } while (NULL != dirp);
719 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
725 for (i = 0; i < g_list_length(g_proc_list); i++) {
726 iter = g_list_nth(g_proc_list, i);
728 if (pid == GPOINTER_TO_INT(iter->data)) {
729 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
736 if (false == exist) {
737 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
738 ttsd_server_finalize(uid);
743 char appid[1024] = {0, };
744 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
745 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
748 if (0 < strlen(appid)) {
749 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
751 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
754 result = ttsdc_send_hello(pid, uid);
757 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) should be removed.", uid);
758 ttsd_server_finalize(uid);
759 } else if (-1 == result) {
760 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
768 Eina_Bool ttsd_cleanup_client(void *data)
770 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
773 if (0 < ttsd_data_get_client_count()) {
774 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
776 if (NULL == g_quit_loop_timer) {
777 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
778 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
780 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
781 g_check_client_timer = NULL;
785 SLOG(LOG_DEBUG, tts_tag(), "@@@");
790 void __used_voice_cb(const char* lang, int type)
792 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
793 if (0 != ttsd_engine_unload_voice(lang, type)) {
794 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
798 int ttsd_server_finalize(unsigned int uid)
800 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
801 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);
803 app_tts_state_e state;
804 if (0 > ttsd_data_get_client_state(uid, &state)) {
805 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
808 ttsd_server_stop(uid);
809 ttsd_player_clear(uid);
811 ttsd_player_destroy_instance(uid);
813 /* Need to unload voice when used voice is unregistered */
814 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
815 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
816 return TTSD_ERROR_OPERATION_FAILED;
819 ttsd_data_delete_client(uid);
821 /* unload engine, if ref count of client is 0 */
822 if (0 == ttsd_data_get_client_count()) {
823 if (NULL == g_quit_loop_timer) {
824 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
825 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
827 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
831 return TTSD_ERROR_NONE;
834 int ttsd_server_set_mode(unsigned int uid, ttsd_mode_e mode)
836 app_tts_state_e state;
837 if (0 > ttsd_data_get_client_state(uid, &state)) {
838 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
839 return TTSD_ERROR_INVALID_PARAMETER;
842 if (APP_STATE_READY != state) {
843 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
844 return TTSD_ERROR_INVALID_STATE;
847 int ret = ttsd_data_set_mode(uid, mode);
849 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
851 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%u), mode(%d)", uid, mode);
857 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)
859 app_tts_state_e state;
860 if (0 > ttsd_data_get_client_state(uid, &state)) {
861 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
862 return TTSD_ERROR_INVALID_PARAMETER;
865 /* check valid voice */
866 char* temp_lang = NULL;
868 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
869 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
870 if (NULL != temp_lang) {
874 return TTSD_ERROR_INVALID_VOICE;
877 if (NULL == temp_lang) {
878 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
879 return TTSD_ERROR_INVALID_VOICE;
882 speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
883 if (NULL == speak_data) {
884 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
885 if (NULL != temp_lang) {
890 return TTSD_ERROR_OPERATION_FAILED;
893 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);
895 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
897 ret = ttsd_data_add_speak_data(uid, speak_data);
899 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
900 if (NULL != temp_lang) {
905 ttsd_data_destroy_speak_data(speak_data);
911 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
912 /* Request load voice */
913 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
914 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
915 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
919 if (NULL != temp_lang) {
924 if (APP_STATE_PLAYING == state) {
925 /* check if engine use network */
926 if (ttsd_engine_agent_need_network()) {
927 if (false == ttsd_network_is_connected()) {
928 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
929 return TTSD_ERROR_OPERATION_FAILED;
933 ttsd_data_set_credential(uid, credential);
935 /* Check whether tts-engine is running or not */
936 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
937 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
938 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
939 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
945 return TTSD_ERROR_NONE;
948 static int __pause_client(unsigned int uid)
950 int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
951 if (TTSD_ERROR_NONE != ret) {
952 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
953 return TTSD_ERROR_OPERATION_FAILED;
956 ret = ttsd_player_pause(uid);
957 if (TTSD_ERROR_NONE != ret) {
958 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
959 return TTSD_ERROR_OPERATION_FAILED;
962 return TTSD_ERROR_NONE;
965 void __send_interrupt_client(unsigned int uid)
967 SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
968 int pid = ttsd_data_get_pid(uid);
970 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
974 app_tts_state_e state = APP_STATE_CREATED;
976 if (0 != ttsd_data_get_client_state(uid, &state)) {
977 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
981 ttsdc_ipc_send_set_state_message(pid, uid, state);
982 SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
986 int ttsd_server_play(unsigned int uid, const char* credential)
988 app_tts_state_e state;
989 if (0 > ttsd_data_get_client_state(uid, &state)) {
990 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
991 return TTSD_ERROR_INVALID_PARAMETER;
994 if (APP_STATE_PLAYING == state) {
995 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
996 return TTSD_ERROR_NONE;
999 /* check if engine use network */
1000 if (ttsd_engine_agent_need_network()) {
1001 if (false == ttsd_network_is_connected()) {
1002 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
1003 return TTSD_ERROR_OUT_OF_NETWORK;
1007 /* check the current playback focus */
1008 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1009 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1010 ttsd_data_clear_data(uid);
1011 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1014 unsigned int current_uid = ttsd_data_get_current_playing();
1015 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1017 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1018 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1019 /* Send interrupt message */
1020 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1023 if (0 != ttsd_server_stop(current_uid)) {
1024 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1027 /* Default mode policy of interrupt is "Pause" */
1029 /* Send interrupt message */
1030 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1033 if (0 != __pause_client(current_uid)) {
1034 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1038 __send_interrupt_client(current_uid);
1041 /* Change current play */
1042 if (0 != ttsd_player_wait_to_play(uid)) {
1043 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1046 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1047 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1048 return TTSD_ERROR_OPERATION_FAILED;
1051 int ret = TTSD_ERROR_NONE;
1052 if (APP_STATE_PAUSED == state) {
1053 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1054 g_is_paused = false;
1055 ret = ttsd_player_resume(uid);
1057 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1058 ret = ttsd_player_play(uid);
1061 if (TTSD_ERROR_NONE != ret) {
1062 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1063 ttsd_data_set_client_state(uid, state);
1064 return TTSD_ERROR_OPERATION_FAILED;
1067 ttsd_data_set_credential(uid, credential);
1069 /* Check whether tts-engine is running or not */
1070 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1071 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1072 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1073 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1074 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1079 return TTSD_ERROR_NONE;
1082 int ttsd_server_stop(unsigned int uid)
1084 app_tts_state_e state;
1085 if (0 > ttsd_data_get_client_state(uid, &state)) {
1086 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1087 return TTSD_ERROR_INVALID_PARAMETER;
1090 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1092 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1093 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1094 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1095 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1096 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1098 int ret = ttsd_engine_cancel_synthesis();
1099 if (TTSD_ERROR_NONE != ret)
1100 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1102 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
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);
1113 g_is_paused = false;
1115 return TTSD_ERROR_NONE;
1118 int ttsd_server_pause(unsigned int uid, int* utt_id)
1120 app_tts_state_e state;
1121 if (0 > ttsd_data_get_client_state(uid, &state)) {
1122 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1123 return TTSD_ERROR_INVALID_PARAMETER;
1126 if (APP_STATE_PLAYING != state) {
1127 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1128 return TTSD_ERROR_INVALID_STATE;
1133 *utt_id = g_utt.uttid;
1134 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1136 int ret = __pause_client(uid);
1137 if (TTSD_ERROR_NONE != ret) {
1138 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : 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 app_tts_state_e state;
1148 if (0 > ttsd_data_get_client_state(uid, &state)) {
1149 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1150 return TTSD_ERROR_INVALID_PARAMETER;
1154 int ret = ttsd_engine_get_voice_list(voice_list);
1156 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1160 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1162 return TTSD_ERROR_NONE;
1165 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1167 app_tts_state_e state;
1168 if (0 > ttsd_data_get_client_state(uid, &state)) {
1169 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1170 return TTSD_ERROR_INVALID_PARAMETER;
1173 /* get current voice */
1174 int ret = ttsd_engine_get_default_voice(language, voice_type);
1176 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1180 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1182 return TTSD_ERROR_NONE;
1185 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1187 app_tts_state_e state;
1188 if (0 > ttsd_data_get_client_state(uid, &state)) {
1189 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1190 return TTSD_ERROR_INVALID_PARAMETER;
1193 if (APP_STATE_READY != state) {
1194 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1195 return TTSD_ERROR_INVALID_STATE;
1198 int ret = ttsd_engine_set_private_data(key, data);
1200 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1202 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1208 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1210 app_tts_state_e state;
1211 if (0 > ttsd_data_get_client_state(uid, &state)) {
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1213 return TTSD_ERROR_INVALID_PARAMETER;
1216 if (APP_STATE_READY != state) {
1217 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1218 return TTSD_ERROR_INVALID_STATE;
1221 int ret = ttsd_engine_get_private_data(key, data);
1223 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1225 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1231 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1233 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1236 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1238 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1244 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1246 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1249 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1251 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1257 int ttsd_server_play_pcm(unsigned int uid)
1259 app_tts_state_e state;
1260 if (0 > ttsd_data_get_client_state(uid, &state)) {
1261 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1262 return TTSD_ERROR_INVALID_PARAMETER;
1265 if (APP_STATE_PLAYING == state) {
1266 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1267 return TTSD_ERROR_NONE;
1270 unsigned int current_uid = ttsd_data_get_current_playing();
1271 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1273 if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1274 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1275 /* Send interrupt message */
1276 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1279 if (0 != ttsd_server_stop(current_uid)) {
1280 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1283 /* Default mode policy of interrupt is "Pause" */
1285 /* Send interrupt message */
1286 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1289 if (0 != __pause_client(current_uid)) {
1290 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1294 __send_interrupt_client(current_uid);
1297 /* Change current play */
1298 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1299 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1300 return TTSD_ERROR_OPERATION_FAILED;
1303 int ret = TTSD_ERROR_NONE;
1304 if (APP_STATE_PAUSED == state) {
1305 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1306 g_is_paused = false;
1307 ret = ttsd_player_resume(uid);
1309 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1310 ret = ttsd_player_play(uid);
1313 if (TTSD_ERROR_NONE != ret) {
1314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1315 ttsd_data_set_client_state(uid, state);
1316 return TTSD_ERROR_OPERATION_FAILED;
1319 return TTSD_ERROR_NONE;
1322 int ttsd_server_stop_pcm(unsigned int uid)
1324 app_tts_state_e state;
1325 if (0 > ttsd_data_get_client_state(uid, &state)) {
1326 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1327 return TTSD_ERROR_INVALID_PARAMETER;
1330 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1332 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1333 ttsd_data_set_client_state(uid, APP_STATE_READY);
1336 /* Reset all data */
1337 ttsd_data_clear_data(uid);
1339 ttsd_player_stop(uid);
1342 return TTSD_ERROR_NONE;
1345 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1347 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1351 /* Synthesis is success */
1352 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1353 if (TTSE_RESULT_EVENT_START == event) {
1354 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1355 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1356 uid, uttid, data, data_size, audio_type, rate);
1357 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1358 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1359 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1360 uid, uttid, data, data_size, audio_type, rate);
1362 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1365 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1366 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1367 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1368 return TTSD_ERROR_INVALID_PARAMETER;
1371 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1372 if (NULL == sound_data) {
1373 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1374 return TTSD_ERROR_OUT_OF_MEMORY;
1377 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1378 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1379 ttsd_data_destroy_sound_data(sound_data);
1382 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1385 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1387 return TTSD_ERROR_NONE;