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;
63 /* Function definitions */
64 static int __synthesis(int uid, const char* credential);
66 static Eina_Bool __wait_synthesis(void *data)
68 /* get current play */
69 char* credential = (char*)data;
70 int uid = ttsd_data_get_current_playing();
73 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
74 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
77 SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
79 if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
80 /* Start next synthesis */
81 __synthesis(uid, credential);
91 static int __synthesis(int uid, const char* credential)
93 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
95 speak_data_s* speak_data = NULL;
96 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
97 if (NULL == speak_data) {
98 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
102 int pid = ttsd_data_get_pid(uid);
104 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d)", uid);
108 char appid[1024] = {0, };
109 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
110 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
113 if (NULL == speak_data->lang || NULL == speak_data->text) {
114 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
115 ttsd_server_stop(uid);
117 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
119 ttsd_data_clear_speak_data(&speak_data);
125 g_utt.uttid = speak_data->utt_id;
127 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
128 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
129 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
130 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
131 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
132 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
135 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
136 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
138 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
140 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
142 ttsd_server_stop(uid);
144 int pid = ttsd_data_get_pid(uid);
146 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
148 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
151 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
154 ttsd_data_clear_speak_data(&speak_data);
156 ttsd_data_clear_speak_data(&speak_data);
159 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
165 * TTS Server Callback Functions
167 int ttsd_send_error(ttse_error_e error, const char* msg)
170 int uttid = g_utt.uttid;
171 int tmp_pid = ttsd_data_get_pid(uid);
173 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%d), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
175 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
177 if (0 != ttsd_player_clear(uid))
178 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
180 if (0 != ttsd_data_clear_data(uid))
181 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
184 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
185 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
188 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
189 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
192 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
193 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
198 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)
200 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
203 int uttid = g_utt.uttid;
205 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
206 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
207 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
208 SLOG(LOG_DEBUG, tts_tag(), "@@@");
209 return TTSD_ERROR_INVALID_PARAMETER;
212 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
213 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
214 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
215 return TTSD_ERROR_INVALID_PARAMETER;
218 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
219 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
220 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
221 SLOG(LOG_DEBUG, tts_tag(), "@@@");
222 return TTSD_ERROR_INVALID_PARAMETER;
225 /* check the current state */
226 app_tts_state_e state;
227 if (0 > ttsd_data_get_client_state(uid, &state)) {
228 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
229 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
230 return TTSD_ERROR_INVALID_PARAMETER;
233 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%d), current state(%d)", uid, state);
234 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
235 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
236 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
237 return TTSD_ERROR_NONE;
240 /* Synthesis is success */
241 if (TTSE_RESULT_EVENT_START == event) {
242 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
243 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
244 uid, uttid, data, data_size, audio_type, rate);
246 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
247 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
248 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
249 double d_latency = (double)latency;
250 g_avg_latency += d_latency;
251 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
252 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
254 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);
255 } else if (TTSE_RESULT_EVENT_FINISH == event) {
256 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
257 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
258 uid, uttid, data, data_size, audio_type, rate);
260 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
264 sound_data_s* temp_sound_data = NULL;
265 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
266 if (NULL == temp_sound_data) {
267 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
268 return TTSD_ERROR_OUT_OF_MEMORY;
271 temp_sound_data->data = NULL;
272 temp_sound_data->rate = 0;
273 temp_sound_data->data_size = 0;
275 if (NULL != data && 0 < data_size) {
276 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
277 if (NULL != temp_sound_data->data) {
278 memcpy(temp_sound_data->data, data, data_size);
279 temp_sound_data->data_size = data_size;
280 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
281 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
283 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
286 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
289 temp_sound_data->utt_id = uttid;
290 temp_sound_data->event = event;
291 temp_sound_data->audio_type = audio_type;
292 temp_sound_data->rate = rate;
294 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
295 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
296 if (NULL != temp_sound_data->data) {
297 free(temp_sound_data->data);
298 temp_sound_data->data = NULL;
301 free(temp_sound_data);
302 temp_sound_data = NULL;
304 return TTSD_ERROR_OPERATION_FAILED;
307 if (event == TTSE_RESULT_EVENT_FINISH) {
308 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
311 return TTSD_ERROR_NONE;
314 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
316 /* clear client data */
317 ttsd_data_clear_data(uid);
319 if (APP_STATE_READY != state) {
320 ttsd_data_set_client_state(uid, APP_STATE_READY);
323 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
325 ttsd_data_delete_client(uid);
332 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);
358 /* stop all player */
359 ttsd_player_all_stop();
361 /* send interrupt message to all clients */
362 ttsd_data_foreach_clients(__get_client_cb, NULL);
364 ttsd_engine_cancel_synthesis();
369 case TTS_CONFIG_TYPE_VOICE:
371 if (NULL == str_param) {
372 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
376 char* out_lang = NULL;
380 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
381 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
382 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
384 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
386 /* Current language is not available */
387 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
389 if (NULL != out_lang) {
396 case TTS_CONFIG_TYPE_SPEED:
398 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
399 /* set default speed */
401 ret = ttsd_engine_agent_set_default_speed(int_param);
403 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
409 case TTS_CONFIG_TYPE_PITCH:
411 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
412 /* set default speed */
414 ret = ttsd_engine_agent_set_default_pitch(int_param);
416 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
422 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
424 if (0.0 <= double_param && double_param <= 1.0) {
425 /* set default bg volume ratio */
427 ret = ttsd_player_set_background_volume_ratio(double_param);
429 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
439 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
441 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
442 ttsd_server_finalize(uid);
446 Eina_Bool ttsd_terminate_daemon(void *data)
448 ttsd_data_foreach_clients(__terminate_client, NULL);
449 g_terminate_timer = NULL;
453 void __screen_reader_changed_cb(bool value)
455 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
456 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
457 if (g_terminate_timer) {
458 ecore_timer_del(g_terminate_timer);
459 g_terminate_timer = NULL;
461 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
464 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
469 int ttsd_send_all_stop()
471 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
473 /* stop all player */
474 ttsd_player_all_stop();
476 /* send interrupt message to all clients */
477 ttsd_data_foreach_clients(__get_client_cb, NULL);
479 return TTSD_ERROR_NONE;
485 int ttsd_initialize(ttse_request_callback_s *callback)
487 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
489 if (ttsd_config_initialize(__config_changed_cb)) {
490 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
494 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
496 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
497 int ret = ttsd_player_set_background_volume_ratio(ratio);
499 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
502 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
505 if (ttsd_player_init()) {
506 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
507 return TTSD_ERROR_OPERATION_FAILED;
510 /* Engine Agent initialize */
511 if (0 != ttsd_engine_agent_init()) {
512 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
513 return TTSD_ERROR_OPERATION_FAILED;
516 /* set current engine */
517 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
518 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
519 // g_is_engine = false;
521 // g_is_engine = true;
523 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
524 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
525 return TTSD_ERROR_OPERATION_FAILED;
528 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
530 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
531 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
534 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
535 if (NULL == g_check_client_timer) {
536 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
539 return TTSD_ERROR_NONE;
544 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
547 if (0 < g_list_length(g_proc_list)) {
548 iter = g_list_first(g_proc_list);
549 while (NULL != iter) {
550 g_proc_list = g_list_remove_link(g_proc_list, iter);
552 iter = g_list_first(g_proc_list);
556 ttsd_config_finalize();
558 ttsd_player_release();
560 ttsd_engine_agent_release();
564 ecore_timer_del(g_wait_timer);
566 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
569 if (g_terminate_timer) {
570 ecore_timer_del(g_terminate_timer);
571 g_terminate_timer = NULL;
572 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
575 if (g_check_client_timer) {
576 ecore_timer_del(g_check_client_timer);
577 g_check_client_timer = NULL;
578 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
581 if (g_quit_loop_timer) {
582 ecore_timer_del(g_quit_loop_timer);
583 g_quit_loop_timer = NULL;
584 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
587 return TTSD_ERROR_NONE;
592 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
594 ttsd_terminate_daemon(NULL);
596 return TTSD_ERROR_NONE;
600 * TTS Server Functions for Client
603 int ttsd_server_is_already_initialized(int pid, int uid, bool* is_initialized)
605 if (-1 != ttsd_data_is_client(uid))
606 *is_initialized = true;
608 *is_initialized = false;
610 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), Uid(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
611 return TTSD_ERROR_NONE;
614 int ttsd_server_initialize(int pid, int uid, tts_ipc_method_e method, bool* credential_needed)
616 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
618 if (-1 != ttsd_data_is_client(uid)) {
619 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
620 return TTSD_ERROR_NONE;
623 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
624 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
625 return TTSD_ERROR_OPERATION_FAILED;
628 if (true == *credential_needed) {
630 if (0 != app_manager_get_app_id(pid, &appid)) {
631 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
633 bool is_agreed = false;
634 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
635 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
638 return TTSD_ERROR_OPERATION_FAILED;
643 if (false == is_agreed) {
644 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
645 return TTSD_ERROR_PERMISSION_DENIED;
649 if (0 != ttsd_data_new_client(pid, uid)) {
650 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
651 return TTSD_ERROR_OPERATION_FAILED;
654 if (0 != ttsd_data_set_ipc_method(uid, method)) {
655 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
656 return TTSD_ERROR_OPERATION_FAILED;
659 if (0 != ttsd_player_create_instance(uid)) {
660 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
661 return TTSD_ERROR_OPERATION_FAILED;
665 g_min_latency = 10000.0;
670 return TTSD_ERROR_NONE;
673 static Eina_Bool __quit_ecore_loop(void *data)
675 ttsd_ipc_close_connection();
676 ttsd_network_finalize();
678 ecore_main_loop_quit();
684 static void __read_proc()
687 struct dirent *dirp = NULL;
691 if (0 < g_list_length(g_proc_list)) {
692 iter = g_list_first(g_proc_list);
693 while (NULL != iter) {
694 g_proc_list = g_list_remove_link(g_proc_list, iter);
696 iter = g_list_first(g_proc_list);
700 dp = opendir("/proc");
702 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
708 tmp = atoi(dirp->d_name);
709 if (0 >= tmp) continue;
710 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
712 } while (NULL != dirp);
718 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
724 for (i = 0; i < g_list_length(g_proc_list); i++) {
725 iter = g_list_nth(g_proc_list, i);
727 if (pid == GPOINTER_TO_INT(iter->data)) {
728 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
735 if (false == exist) {
736 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
737 ttsd_server_finalize(uid);
742 char appid[1024] = {0, };
743 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
744 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
747 if (0 < strlen(appid)) {
748 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
750 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
753 result = ttsdc_send_hello(pid, uid);
756 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
757 ttsd_server_finalize(uid);
758 } else if (-1 == result) {
759 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
767 Eina_Bool ttsd_cleanup_client(void *data)
769 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
772 if (0 < ttsd_data_get_client_count()) {
773 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
775 if (NULL == g_quit_loop_timer) {
776 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
777 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
779 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
780 g_check_client_timer = NULL;
784 SLOG(LOG_DEBUG, tts_tag(), "@@@");
789 void __used_voice_cb(const char* lang, int type)
791 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
792 if (0 != ttsd_engine_unload_voice(lang, type)) {
793 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
797 int ttsd_server_finalize(int uid)
799 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
800 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);
802 app_tts_state_e state;
803 if (0 > ttsd_data_get_client_state(uid, &state)) {
804 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
807 ttsd_server_stop(uid);
808 ttsd_player_clear(uid);
810 ttsd_player_destroy_instance(uid);
812 /* Need to unload voice when used voice is unregistered */
813 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
814 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
815 return TTSD_ERROR_OPERATION_FAILED;
818 ttsd_data_delete_client(uid);
820 /* unload engine, if ref count of client is 0 */
821 if (0 == ttsd_data_get_client_count()) {
822 if (NULL == g_quit_loop_timer) {
823 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
824 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
826 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
830 return TTSD_ERROR_NONE;
833 int ttsd_server_set_mode(int uid, ttsd_mode_e mode)
835 app_tts_state_e state;
836 if (0 > ttsd_data_get_client_state(uid, &state)) {
837 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
838 return TTSD_ERROR_INVALID_PARAMETER;
841 if (APP_STATE_READY != state) {
842 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
843 return TTSD_ERROR_INVALID_STATE;
846 int ret = ttsd_data_set_mode(uid, mode);
848 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
850 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%d), mode(%d)", uid, mode);
856 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
858 app_tts_state_e state;
859 if (0 > ttsd_data_get_client_state(uid, &state)) {
860 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
861 return TTSD_ERROR_INVALID_PARAMETER;
864 /* check valid voice */
865 char* temp_lang = NULL;
867 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
868 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
869 if (NULL != temp_lang) {
873 return TTSD_ERROR_INVALID_VOICE;
876 if (NULL == temp_lang) {
877 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
878 return TTSD_ERROR_INVALID_VOICE;
881 speak_data_s* speak_data = NULL;
882 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
883 if (NULL == speak_data) {
884 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
885 if (NULL != temp_lang) {
889 return TTSD_ERROR_OPERATION_FAILED;
892 speak_data->lang = strdup(lang);
893 speak_data->vctype = voice_type;
895 speak_data->speed = speed;
896 speak_data->utt_id = utt_id;
898 speak_data->text = strdup(text);
900 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);
902 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
904 ret = ttsd_data_add_speak_data(uid, speak_data);
906 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
907 if (NULL != temp_lang) {
911 if (NULL != speak_data) {
912 if (NULL != speak_data->lang) free(speak_data->lang);
913 if (NULL != speak_data->text) free(speak_data->text);
915 speak_data->lang = NULL;
916 speak_data->text = NULL;
925 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
926 /* Request load voice */
927 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
928 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
929 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
933 if (NULL != temp_lang) {
938 if (APP_STATE_PLAYING == state) {
939 /* check if engine use network */
940 if (ttsd_engine_agent_need_network()) {
941 if (false == ttsd_network_is_connected()) {
942 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
943 return TTSD_ERROR_OPERATION_FAILED;
947 /* Check whether tts-engine is running or not */
948 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
949 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
950 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
951 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
953 __synthesis(uid, credential);
957 return TTSD_ERROR_NONE;
960 Eina_Bool __send_interrupt_client(void *data)
962 intptr_t puid = (intptr_t)data;
965 int pid = ttsd_data_get_pid(uid);
967 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
971 ttsd_mode_e mode = ttsd_data_get_mode(uid);
972 if (TTSD_MODE_DEFAULT != mode) {
973 /* send message to client about changing state */
974 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
976 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
982 int ttsd_server_play(int uid, const char* credential)
984 app_tts_state_e state;
985 if (0 > ttsd_data_get_client_state(uid, &state)) {
986 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
987 return TTSD_ERROR_INVALID_PARAMETER;
990 if (APP_STATE_PLAYING == state) {
991 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
992 return TTSD_ERROR_NONE;
995 /* check if engine use network */
996 if (ttsd_engine_agent_need_network()) {
997 if (false == ttsd_network_is_connected()) {
998 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
999 return TTSD_ERROR_OUT_OF_NETWORK;
1003 /* check the current playback focus */
1004 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1005 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1006 ttsd_data_clear_data(uid);
1007 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1010 int current_uid = ttsd_data_get_current_playing();
1011 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1013 if (uid != current_uid && -1 != current_uid) {
1014 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1015 /* Send interrupt message */
1016 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1019 if (0 != ttsd_server_stop(current_uid)) {
1020 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1023 intptr_t pcurrent_uid = (intptr_t)current_uid;
1024 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1026 /* Default mode policy of interrupt is "Pause" */
1028 /* Send interrupt message */
1029 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1032 if (0 != ttsd_player_pause(current_uid)) {
1033 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1037 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1039 intptr_t pcurrent_uid = (intptr_t)current_uid;
1040 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1044 /* Change current play */
1045 if (0 != ttsd_player_wait_to_play(uid)) {
1046 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1049 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1050 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1051 return TTSD_ERROR_OPERATION_FAILED;
1054 if (APP_STATE_PAUSED == state) {
1055 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1057 g_is_paused = false;
1060 if (0 != ttsd_player_resume(uid)) {
1061 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1064 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%d)", uid);
1066 if (0 != ttsd_player_play(uid)) {
1067 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1071 /* Check whether tts-engine is running or not */
1072 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1073 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1074 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1075 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1076 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1078 __synthesis(uid, credential);
1081 return TTSD_ERROR_NONE;
1084 int ttsd_server_stop(int uid)
1086 app_tts_state_e state;
1087 if (0 > ttsd_data_get_client_state(uid, &state)) {
1088 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1089 return TTSD_ERROR_INVALID_PARAMETER;
1092 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1094 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1095 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1096 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1097 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1098 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1101 ret = ttsd_engine_cancel_synthesis();
1103 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1105 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1108 if (0 != ttsd_player_stop(uid)) {
1109 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1112 ttsd_data_set_client_state(uid, APP_STATE_READY);
1115 /* Reset all data */
1116 ttsd_data_clear_data(uid);
1117 g_is_paused = false;
1119 return TTSD_ERROR_NONE;
1122 int ttsd_server_pause(int uid, int* utt_id)
1124 app_tts_state_e state;
1125 if (0 > ttsd_data_get_client_state(uid, &state)) {
1126 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1127 return TTSD_ERROR_INVALID_PARAMETER;
1130 if (APP_STATE_PLAYING != state) {
1131 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1132 return TTSD_ERROR_INVALID_STATE;
1137 *utt_id = g_utt.uttid;
1138 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1141 ret = ttsd_player_pause(uid);
1143 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1144 return TTSD_ERROR_OPERATION_FAILED;
1147 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1149 return TTSD_ERROR_NONE;
1152 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1154 app_tts_state_e state;
1155 if (0 > ttsd_data_get_client_state(uid, &state)) {
1156 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1157 return TTSD_ERROR_INVALID_PARAMETER;
1161 int ret = ttsd_engine_get_voice_list(voice_list);
1163 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1167 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1169 return TTSD_ERROR_NONE;
1172 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1174 app_tts_state_e state;
1175 if (0 > ttsd_data_get_client_state(uid, &state)) {
1176 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1177 return TTSD_ERROR_INVALID_PARAMETER;
1180 /* get current voice */
1181 int ret = ttsd_engine_get_default_voice(language, voice_type);
1183 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1187 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1189 return TTSD_ERROR_NONE;
1192 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1194 app_tts_state_e state;
1195 if (0 > ttsd_data_get_client_state(uid, &state)) {
1196 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1197 return TTSD_ERROR_INVALID_PARAMETER;
1200 if (APP_STATE_READY != state) {
1201 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1202 return TTSD_ERROR_INVALID_STATE;
1205 int ret = ttsd_engine_set_private_data(key, data);
1207 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1209 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1215 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1217 app_tts_state_e state;
1218 if (0 > ttsd_data_get_client_state(uid, &state)) {
1219 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1220 return TTSD_ERROR_INVALID_PARAMETER;
1223 if (APP_STATE_READY != state) {
1224 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1225 return TTSD_ERROR_INVALID_STATE;
1228 int ret = ttsd_engine_get_private_data(key, data);
1230 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1232 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1238 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1240 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1243 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1245 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1251 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1253 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1256 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1258 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1264 int ttsd_server_play_pcm(int uid)
1266 app_tts_state_e state;
1267 if (0 > ttsd_data_get_client_state(uid, &state)) {
1268 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1269 return TTSD_ERROR_INVALID_PARAMETER;
1272 if (APP_STATE_PLAYING == state) {
1273 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1274 return TTSD_ERROR_NONE;
1277 int current_uid = ttsd_data_get_current_playing();
1278 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1280 if (uid != current_uid && -1 != current_uid) {
1281 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1282 /* Send interrupt message */
1283 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1286 if (0 != ttsd_server_stop(current_uid)) {
1287 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1290 intptr_t pcurrent_uid = (intptr_t)current_uid;
1291 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1293 /* Default mode policy of interrupt is "Pause" */
1295 /* Send interrupt message */
1296 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1299 if (0 != ttsd_player_pause(current_uid)) {
1300 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1304 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1306 intptr_t pcurrent_uid = (intptr_t)current_uid;
1307 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1311 /* Change current play */
1312 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1313 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1314 return TTSD_ERROR_OPERATION_FAILED;
1317 if (APP_STATE_PAUSED == state) {
1318 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1321 if (0 != ttsd_player_resume(uid)) {
1322 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1325 if (0 != ttsd_player_play(uid)) {
1326 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1328 // Change ready state
1329 ttsd_server_stop_pcm(uid);
1331 int tmp_pid = ttsd_data_get_pid(uid);
1333 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1335 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1340 return TTSD_ERROR_NONE;
1343 int ttsd_server_stop_pcm(int uid)
1345 app_tts_state_e state;
1346 if (0 > ttsd_data_get_client_state(uid, &state)) {
1347 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1348 return TTSD_ERROR_INVALID_PARAMETER;
1351 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1353 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1354 ttsd_data_set_client_state(uid, APP_STATE_READY);
1357 /* Reset all data */
1358 ttsd_data_clear_data(uid);
1360 ttsd_player_stop(uid);
1363 return TTSD_ERROR_NONE;
1366 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1368 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1372 /* Synthesis is success */
1373 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1374 if (TTSE_RESULT_EVENT_START == event) {
1375 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1376 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1377 uid, uttid, data, data_size, audio_type, rate);
1378 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1379 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1380 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1381 uid, uttid, data, data_size, audio_type, rate);
1383 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1386 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1387 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1388 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1389 return TTSD_ERROR_INVALID_PARAMETER;
1393 sound_data_s* temp_sound_data = NULL;
1394 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1395 if (NULL == temp_sound_data) {
1396 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1397 return TTSD_ERROR_OUT_OF_MEMORY;
1400 temp_sound_data->data = NULL;
1401 temp_sound_data->rate = 0;
1402 temp_sound_data->data_size = 0;
1404 if (0 < data_size) {
1405 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1406 if (NULL != temp_sound_data->data) {
1407 memcpy(temp_sound_data->data, data, data_size);
1408 temp_sound_data->data_size = data_size;
1409 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1410 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1412 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1415 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1418 temp_sound_data->utt_id = uttid;
1419 temp_sound_data->event = event;
1420 temp_sound_data->audio_type = audio_type;
1421 temp_sound_data->rate = rate;
1423 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1424 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1426 if (NULL != temp_sound_data->data) {
1427 free(temp_sound_data->data);
1428 temp_sound_data->data = NULL;
1431 free(temp_sound_data);
1432 temp_sound_data = NULL;
1435 /* if (0 != ttsd_player_play(uid)) {
1436 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1438 // Change ready state
1439 ttsd_server_stop(uid);
1442 tmp_pid = ttsd_data_get_pid(uid);
1443 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1447 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1450 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1452 return TTSD_ERROR_NONE;