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 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
77 if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
78 /* Start next synthesis */
79 __synthesis(uid, credential);
89 static int __synthesis(int uid, const char* credential)
91 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS START");
93 speak_data_s* speak_data = NULL;
94 if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
95 if (NULL == speak_data) {
96 SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
100 int pid = ttsd_data_get_pid(uid);
102 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d)", uid);
106 char appid[1024] = {0, };
107 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
108 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
111 if (NULL == speak_data->lang || NULL == speak_data->text) {
112 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
113 ttsd_server_stop(uid);
115 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
117 ttsd_data_clear_speak_data(&speak_data);
123 g_utt.uttid = speak_data->utt_id;
125 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
126 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
127 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
128 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
129 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
130 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
133 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
134 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
136 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
138 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
140 ttsd_server_stop(uid);
142 int pid = ttsd_data_get_pid(uid);
144 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
146 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
149 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
152 ttsd_data_clear_speak_data(&speak_data);
154 ttsd_data_clear_speak_data(&speak_data);
157 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END");
163 * TTS Server Callback Functions
165 int ttsd_send_error(ttse_error_e error, const char* msg)
168 int uttid = g_utt.uttid;
169 int tmp_pid = ttsd_data_get_pid(uid);
171 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));
173 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
175 if (0 != ttsd_player_clear(uid))
176 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_clear()");
178 if (0 != ttsd_data_clear_data(uid))
179 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
182 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
183 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
186 if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
187 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
190 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
191 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
196 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)
198 SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
201 int uttid = g_utt.uttid;
203 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
204 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
205 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
206 SLOG(LOG_DEBUG, tts_tag(), "@@@");
207 return TTSD_ERROR_INVALID_PARAMETER;
210 if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
211 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
212 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
213 return TTSD_ERROR_INVALID_PARAMETER;
216 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
217 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
218 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
219 SLOG(LOG_DEBUG, tts_tag(), "@@@");
220 return TTSD_ERROR_INVALID_PARAMETER;
223 /* check the current state */
224 app_tts_state_e state;
225 if (0 > ttsd_data_get_client_state(uid, &state)) {
226 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
227 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
228 return TTSD_ERROR_INVALID_PARAMETER;
231 SLOG(LOG_INFO, tts_tag(), "[Server] uid(%d), current state(%d)", uid, state);
232 if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
233 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
234 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
235 return TTSD_ERROR_NONE;
238 /* Synthesis is success */
239 if (TTSE_RESULT_EVENT_START == event) {
240 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
241 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
242 uid, uttid, data, data_size, audio_type, rate);
244 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
245 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
246 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
247 double d_latency = (double)latency;
248 g_avg_latency += d_latency;
249 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
250 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
252 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);
253 } else if (TTSE_RESULT_EVENT_FINISH == event) {
254 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
255 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
256 uid, uttid, data, data_size, audio_type, rate);
258 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
262 sound_data_s* temp_sound_data = NULL;
263 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
264 if (NULL == temp_sound_data) {
265 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
266 return TTSD_ERROR_OUT_OF_MEMORY;
269 temp_sound_data->data = NULL;
270 temp_sound_data->rate = 0;
271 temp_sound_data->data_size = 0;
273 if (NULL != data && 0 < data_size) {
274 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
275 if (NULL != temp_sound_data->data) {
276 memcpy(temp_sound_data->data, data, data_size);
277 temp_sound_data->data_size = data_size;
278 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
279 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
281 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
284 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
287 temp_sound_data->utt_id = uttid;
288 temp_sound_data->event = event;
289 temp_sound_data->audio_type = audio_type;
290 temp_sound_data->rate = rate;
292 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
293 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
294 if (NULL != temp_sound_data->data) {
295 free(temp_sound_data->data);
296 temp_sound_data->data = NULL;
299 free(temp_sound_data);
300 temp_sound_data = NULL;
302 return TTSD_ERROR_OPERATION_FAILED;
305 if (event == TTSE_RESULT_EVENT_FINISH) {
306 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
309 return TTSD_ERROR_NONE;
312 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
314 /* clear client data */
315 ttsd_data_clear_data(uid);
317 if (APP_STATE_READY != state) {
318 ttsd_data_set_client_state(uid, APP_STATE_READY);
321 if (0 != ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY)) {
323 ttsd_data_delete_client(uid);
330 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
333 case TTS_CONFIG_TYPE_ENGINE:
335 /* TODO - Determine the policy when engine process get engine changed cb */
336 if (NULL == str_param) {
337 SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
342 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
343 SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
344 ret = ttsd_engine_agent_unload_current_engine();
346 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
349 ret = ttsd_engine_agent_load_current_engine();
351 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
356 /* stop all player */
357 ttsd_player_all_stop();
359 /* send interrupt message to all clients */
360 ttsd_data_foreach_clients(__get_client_cb, NULL);
362 ttsd_engine_cancel_synthesis();
367 case TTS_CONFIG_TYPE_VOICE:
369 if (NULL == str_param) {
370 SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
374 char* out_lang = NULL;
378 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
379 SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
380 ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
382 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
384 /* Current language is not available */
385 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
387 if (NULL != out_lang) {
394 case TTS_CONFIG_TYPE_SPEED:
396 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
397 /* set default speed */
399 ret = ttsd_engine_agent_set_default_speed(int_param);
401 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
407 case TTS_CONFIG_TYPE_PITCH:
409 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
410 /* set default speed */
412 ret = ttsd_engine_agent_set_default_pitch(int_param);
414 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
420 case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
422 if (0.0 <= double_param && double_param <= 1.0) {
423 /* set default bg volume ratio */
425 ret = ttsd_player_set_background_volume_ratio(double_param);
427 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
437 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
439 SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
440 ttsd_server_finalize(uid);
444 Eina_Bool ttsd_terminate_daemon(void *data)
446 ttsd_data_foreach_clients(__terminate_client, NULL);
447 g_terminate_timer = NULL;
451 void __screen_reader_changed_cb(bool value)
453 /* if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
454 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
455 if (g_terminate_timer) {
456 ecore_timer_del(g_terminate_timer);
457 g_terminate_timer = NULL;
459 g_terminate_timer = ecore_timer_add(1, ttsd_terminate_daemon, NULL);
462 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
467 int ttsd_send_all_stop()
469 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
471 /* stop all player */
472 ttsd_player_all_stop();
474 /* send interrupt message to all clients */
475 ttsd_data_foreach_clients(__get_client_cb, NULL);
477 return TTSD_ERROR_NONE;
483 int ttsd_initialize(ttse_request_callback_s *callback)
485 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
487 if (ttsd_config_initialize(__config_changed_cb)) {
488 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
492 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
494 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
495 int ret = ttsd_player_set_background_volume_ratio(ratio);
497 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
500 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
503 if (ttsd_player_init()) {
504 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
505 return TTSD_ERROR_OPERATION_FAILED;
508 /* Engine Agent initialize */
509 if (0 != ttsd_engine_agent_init()) {
510 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
511 return TTSD_ERROR_OPERATION_FAILED;
514 /* set current engine */
515 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
516 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
517 // g_is_engine = false;
519 // g_is_engine = true;
521 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
522 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
523 return TTSD_ERROR_OPERATION_FAILED;
526 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
528 // if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
529 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
532 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
533 if (NULL == g_check_client_timer) {
534 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
537 return TTSD_ERROR_NONE;
542 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
545 if (0 < g_list_length(g_proc_list)) {
546 iter = g_list_first(g_proc_list);
547 while (NULL != iter) {
548 g_proc_list = g_list_remove_link(g_proc_list, iter);
550 iter = g_list_first(g_proc_list);
554 ttsd_config_finalize();
556 ttsd_player_release();
558 ttsd_engine_agent_release();
562 ecore_timer_del(g_wait_timer);
564 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
567 if (g_terminate_timer) {
568 ecore_timer_del(g_terminate_timer);
569 g_terminate_timer = NULL;
570 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
573 if (g_check_client_timer) {
574 ecore_timer_del(g_check_client_timer);
575 g_check_client_timer = NULL;
576 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
579 if (g_quit_loop_timer) {
580 ecore_timer_del(g_quit_loop_timer);
581 g_quit_loop_timer = NULL;
582 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
585 return TTSD_ERROR_NONE;
590 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
592 ttsd_terminate_daemon(NULL);
594 return TTSD_ERROR_NONE;
598 * TTS Server Functions for Client
601 int ttsd_server_is_already_initialized(int pid, int uid, bool* is_initialized)
603 if (-1 != ttsd_data_is_client(uid))
604 *is_initialized = true;
606 *is_initialized = false;
608 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), Uid(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
609 return TTSD_ERROR_NONE;
612 int ttsd_server_initialize(int pid, int uid, tts_ipc_method_e method, bool* credential_needed)
614 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
616 if (-1 != ttsd_data_is_client(uid)) {
617 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
618 return TTSD_ERROR_NONE;
621 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
622 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
623 return TTSD_ERROR_OPERATION_FAILED;
626 if (true == *credential_needed) {
628 if (0 != app_manager_get_app_id(pid, &appid)) {
629 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
631 bool is_agreed = false;
632 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
633 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
636 return TTSD_ERROR_OPERATION_FAILED;
641 if (false == is_agreed) {
642 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
643 return TTSD_ERROR_PERMISSION_DENIED;
647 if (0 != ttsd_data_new_client(pid, uid)) {
648 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
649 return TTSD_ERROR_OPERATION_FAILED;
652 if (0 != ttsd_data_set_ipc_method(uid, method)) {
653 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
654 return TTSD_ERROR_OPERATION_FAILED;
657 if (0 != ttsd_player_create_instance(uid)) {
658 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
659 return TTSD_ERROR_OPERATION_FAILED;
663 g_min_latency = 10000.0;
668 return TTSD_ERROR_NONE;
671 static Eina_Bool __quit_ecore_loop(void *data)
673 ttsd_ipc_close_connection();
674 ttsd_network_finalize();
676 ecore_main_loop_quit();
682 static void __read_proc()
685 struct dirent *dirp = NULL;
689 if (0 < g_list_length(g_proc_list)) {
690 iter = g_list_first(g_proc_list);
691 while (NULL != iter) {
692 g_proc_list = g_list_remove_link(g_proc_list, iter);
694 iter = g_list_first(g_proc_list);
698 dp = opendir("/proc");
700 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
706 tmp = atoi(dirp->d_name);
707 if (0 >= tmp) continue;
708 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
710 } while (NULL != dirp);
716 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
722 for (i = 0; i < g_list_length(g_proc_list); i++) {
723 iter = g_list_nth(g_proc_list, i);
725 if (pid == GPOINTER_TO_INT(iter->data)) {
726 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
733 if (false == exist) {
734 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
735 ttsd_server_finalize(uid);
740 char appid[1024] = {0, };
741 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
742 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
745 if (0 < strlen(appid)) {
746 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
748 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
751 result = ttsdc_send_hello(pid, uid);
754 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
755 ttsd_server_finalize(uid);
756 } else if (-1 == result) {
757 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
765 Eina_Bool ttsd_cleanup_client(void *data)
767 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
770 if (0 < ttsd_data_get_client_count()) {
771 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
773 if (NULL == g_quit_loop_timer) {
774 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
775 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
777 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
778 g_check_client_timer = NULL;
782 SLOG(LOG_DEBUG, tts_tag(), "@@@");
787 void __used_voice_cb(const char* lang, int type)
789 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
790 if (0 != ttsd_engine_unload_voice(lang, type)) {
791 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
795 int ttsd_server_finalize(int uid)
797 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
798 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);
800 app_tts_state_e state;
801 if (0 > ttsd_data_get_client_state(uid, &state)) {
802 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
805 ttsd_server_stop(uid);
806 ttsd_player_clear(uid);
808 ttsd_player_destroy_instance(uid);
810 /* Need to unload voice when used voice is unregistered */
811 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
812 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
813 return TTSD_ERROR_OPERATION_FAILED;
816 ttsd_data_delete_client(uid);
818 /* unload engine, if ref count of client is 0 */
819 if (0 == ttsd_data_get_client_count()) {
820 if (NULL == g_quit_loop_timer) {
821 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
822 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
824 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
828 return TTSD_ERROR_NONE;
831 int ttsd_server_set_mode(int uid, ttsd_mode_e mode)
833 app_tts_state_e state;
834 if (0 > ttsd_data_get_client_state(uid, &state)) {
835 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
836 return TTSD_ERROR_INVALID_PARAMETER;
839 if (APP_STATE_READY != state) {
840 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
841 return TTSD_ERROR_INVALID_STATE;
844 int ret = ttsd_data_set_mode(uid, mode);
846 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set mode : ret(%d)", ret);
848 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set mode. uid(%d), mode(%d)", uid, mode);
854 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
856 app_tts_state_e state;
857 if (0 > ttsd_data_get_client_state(uid, &state)) {
858 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
859 return TTSD_ERROR_INVALID_PARAMETER;
862 /* check valid voice */
863 char* temp_lang = NULL;
865 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
866 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
867 if (NULL != temp_lang) {
871 return TTSD_ERROR_INVALID_VOICE;
874 if (NULL == temp_lang) {
875 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
876 return TTSD_ERROR_INVALID_VOICE;
879 speak_data_s* speak_data = NULL;
880 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
881 if (NULL == speak_data) {
882 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
883 if (NULL != temp_lang) {
887 return TTSD_ERROR_OPERATION_FAILED;
890 speak_data->lang = strdup(lang);
891 speak_data->vctype = voice_type;
893 speak_data->speed = speed;
894 speak_data->utt_id = utt_id;
896 speak_data->text = strdup(text);
898 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);
900 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
902 ret = ttsd_data_add_speak_data(uid, speak_data);
904 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
905 if (NULL != temp_lang) {
909 if (NULL != speak_data) {
910 if (NULL != speak_data->lang) free(speak_data->lang);
911 if (NULL != speak_data->text) free(speak_data->text);
913 speak_data->lang = NULL;
914 speak_data->text = NULL;
923 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
924 /* Request load voice */
925 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
926 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
927 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
931 if (NULL != temp_lang) {
936 if (APP_STATE_PLAYING == state) {
937 /* check if engine use network */
938 if (ttsd_engine_agent_need_network()) {
939 if (false == ttsd_network_is_connected()) {
940 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
941 return TTSD_ERROR_OPERATION_FAILED;
945 /* Check whether tts-engine is running or not */
946 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
947 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
949 __synthesis(uid, credential);
953 return TTSD_ERROR_NONE;
956 Eina_Bool __send_interrupt_client(void *data)
958 intptr_t puid = (intptr_t)data;
961 int pid = ttsd_data_get_pid(uid);
963 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
967 ttsd_mode_e mode = ttsd_data_get_mode(uid);
968 if (TTSD_MODE_DEFAULT != mode) {
969 /* send message to client about changing state */
970 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
972 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
978 int ttsd_server_play(int uid, const char* credential)
980 app_tts_state_e state;
981 if (0 > ttsd_data_get_client_state(uid, &state)) {
982 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
983 return TTSD_ERROR_INVALID_PARAMETER;
986 if (APP_STATE_PLAYING == state) {
987 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
988 return TTSD_ERROR_NONE;
991 /* check if engine use network */
992 if (ttsd_engine_agent_need_network()) {
993 if (false == ttsd_network_is_connected()) {
994 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
995 return TTSD_ERROR_OUT_OF_NETWORK;
999 /* check the current playback focus */
1000 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
1001 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
1002 ttsd_data_clear_data(uid);
1003 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
1006 int current_uid = ttsd_data_get_current_playing();
1007 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1009 if (uid != current_uid && -1 != current_uid) {
1010 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1011 /* Send interrupt message */
1012 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1015 if (0 != ttsd_server_stop(current_uid)) {
1016 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1019 intptr_t pcurrent_uid = (intptr_t)current_uid;
1020 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1022 /* Default mode policy of interrupt is "Pause" */
1024 /* Send interrupt message */
1025 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1028 if (0 != ttsd_player_pause(current_uid)) {
1029 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1033 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1035 intptr_t pcurrent_uid = (intptr_t)current_uid;
1036 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1040 /* Change current play */
1041 if (0 != ttsd_player_wait_to_play(uid)) {
1042 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1045 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1046 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1047 return TTSD_ERROR_OPERATION_FAILED;
1050 if (APP_STATE_PAUSED == state) {
1051 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1053 g_is_paused = false;
1056 if (0 != ttsd_player_resume(uid)) {
1057 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1060 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%d)", uid);
1062 if (0 != ttsd_player_play(uid)) {
1063 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1067 /* Check whether tts-engine is running or not */
1068 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1069 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
1070 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1072 __synthesis(uid, credential);
1075 return TTSD_ERROR_NONE;
1078 int ttsd_server_stop(int uid)
1080 app_tts_state_e state;
1081 if (0 > ttsd_data_get_client_state(uid, &state)) {
1082 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1083 return TTSD_ERROR_INVALID_PARAMETER;
1086 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1088 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1089 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
1090 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1093 ret = ttsd_engine_cancel_synthesis();
1095 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1097 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1100 if (0 != ttsd_player_stop(uid)) {
1101 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1104 ttsd_data_set_client_state(uid, APP_STATE_READY);
1107 /* Reset all data */
1108 ttsd_data_clear_data(uid);
1109 g_is_paused = false;
1111 return TTSD_ERROR_NONE;
1114 int ttsd_server_pause(int uid, int* utt_id)
1116 app_tts_state_e state;
1117 if (0 > ttsd_data_get_client_state(uid, &state)) {
1118 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1119 return TTSD_ERROR_INVALID_PARAMETER;
1122 if (APP_STATE_PLAYING != state) {
1123 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1124 return TTSD_ERROR_INVALID_STATE;
1129 *utt_id = g_utt.uttid;
1130 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1133 ret = ttsd_player_pause(uid);
1135 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1136 return TTSD_ERROR_OPERATION_FAILED;
1139 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1141 return TTSD_ERROR_NONE;
1144 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1146 app_tts_state_e state;
1147 if (0 > ttsd_data_get_client_state(uid, &state)) {
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(int uid, char** language, int* voice_type)
1166 app_tts_state_e state;
1167 if (0 > ttsd_data_get_client_state(uid, &state)) {
1168 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1169 return TTSD_ERROR_INVALID_PARAMETER;
1172 /* get current voice */
1173 int ret = ttsd_engine_get_default_voice(language, voice_type);
1175 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1179 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1181 return TTSD_ERROR_NONE;
1184 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1186 app_tts_state_e state;
1187 if (0 > ttsd_data_get_client_state(uid, &state)) {
1188 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1189 return TTSD_ERROR_INVALID_PARAMETER;
1192 if (APP_STATE_READY != state) {
1193 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1194 return TTSD_ERROR_INVALID_STATE;
1197 int ret = ttsd_engine_set_private_data(key, data);
1199 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1201 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1207 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1209 app_tts_state_e state;
1210 if (0 > ttsd_data_get_client_state(uid, &state)) {
1211 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1212 return TTSD_ERROR_INVALID_PARAMETER;
1215 if (APP_STATE_READY != state) {
1216 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1217 return TTSD_ERROR_INVALID_STATE;
1220 int ret = ttsd_engine_get_private_data(key, 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);
1230 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1232 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1235 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1237 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1243 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1245 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1248 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1250 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1256 int ttsd_server_play_pcm(int uid)
1258 app_tts_state_e state;
1259 if (0 > ttsd_data_get_client_state(uid, &state)) {
1260 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1261 return TTSD_ERROR_INVALID_PARAMETER;
1264 if (APP_STATE_PLAYING == state) {
1265 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1266 return TTSD_ERROR_NONE;
1269 int current_uid = ttsd_data_get_current_playing();
1270 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1272 if (uid != current_uid && -1 != current_uid) {
1273 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1274 /* Send interrupt message */
1275 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1278 if (0 != ttsd_server_stop(current_uid)) {
1279 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1282 intptr_t pcurrent_uid = (intptr_t)current_uid;
1283 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1285 /* Default mode policy of interrupt is "Pause" */
1287 /* Send interrupt message */
1288 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1291 if (0 != ttsd_player_pause(current_uid)) {
1292 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1296 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1298 intptr_t pcurrent_uid = (intptr_t)current_uid;
1299 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1303 /* Change current play */
1304 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1305 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1306 return TTSD_ERROR_OPERATION_FAILED;
1309 if (APP_STATE_PAUSED == state) {
1310 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1313 if (0 != ttsd_player_resume(uid)) {
1314 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1317 if (0 != ttsd_player_play(uid)) {
1318 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1320 // Change ready state
1321 ttsd_server_stop_pcm(uid);
1323 int tmp_pid = ttsd_data_get_pid(uid);
1325 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1327 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1332 return TTSD_ERROR_NONE;
1335 int ttsd_server_stop_pcm(int uid)
1337 app_tts_state_e state;
1338 if (0 > ttsd_data_get_client_state(uid, &state)) {
1339 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1340 return TTSD_ERROR_INVALID_PARAMETER;
1343 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1345 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1346 ttsd_data_set_client_state(uid, APP_STATE_READY);
1349 /* Reset all data */
1350 ttsd_data_clear_data(uid);
1352 ttsd_player_stop(uid);
1355 return TTSD_ERROR_NONE;
1358 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1360 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1364 /* Synthesis is success */
1365 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1366 if (TTSE_RESULT_EVENT_START == event) {
1367 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1368 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1369 uid, uttid, data, data_size, audio_type, rate);
1370 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1371 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1372 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1373 uid, uttid, data, data_size, audio_type, rate);
1375 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1378 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1379 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1380 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1381 return TTSD_ERROR_INVALID_PARAMETER;
1385 sound_data_s* temp_sound_data = NULL;
1386 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1387 if (NULL == temp_sound_data) {
1388 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1389 return TTSD_ERROR_OUT_OF_MEMORY;
1392 temp_sound_data->data = NULL;
1393 temp_sound_data->rate = 0;
1394 temp_sound_data->data_size = 0;
1396 if (0 < data_size) {
1397 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1398 if (NULL != temp_sound_data->data) {
1399 memcpy(temp_sound_data->data, data, data_size);
1400 temp_sound_data->data_size = data_size;
1401 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1402 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1404 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1407 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1410 temp_sound_data->utt_id = uttid;
1411 temp_sound_data->event = event;
1412 temp_sound_data->audio_type = audio_type;
1413 temp_sound_data->rate = rate;
1415 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1416 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1418 if (NULL != temp_sound_data->data) {
1419 free(temp_sound_data->data);
1420 temp_sound_data->data = NULL;
1423 free(temp_sound_data);
1424 temp_sound_data = NULL;
1427 /* if (0 != ttsd_player_play(uid)) {
1428 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1430 // Change ready state
1431 ttsd_server_stop(uid);
1434 tmp_pid = ttsd_data_get_pid(uid);
1435 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1439 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1442 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1444 return TTSD_ERROR_NONE;