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);
461 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
466 int ttsd_send_all_stop()
468 SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
470 /* stop all player */
471 ttsd_player_all_stop();
473 /* send interrupt message to all clients */
474 ttsd_data_foreach_clients(__get_client_cb, NULL);
476 return TTSD_ERROR_NONE;
482 int ttsd_initialize(ttse_request_callback_s *callback)
484 SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
486 if (ttsd_config_initialize(__config_changed_cb)) {
487 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
491 if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
493 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
494 int ret = ttsd_player_set_background_volume_ratio(ratio);
496 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
499 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
502 if (ttsd_player_init()) {
503 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
504 return TTSD_ERROR_OPERATION_FAILED;
507 /* Engine Agent initialize */
508 if (0 != ttsd_engine_agent_init()) {
509 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
510 return TTSD_ERROR_OPERATION_FAILED;
513 /* set current engine */
514 //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
515 // SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
516 // g_is_engine = false;
518 // g_is_engine = true;
520 if (0 != ttsd_engine_agent_load_current_engine(callback)) {
521 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
522 return TTSD_ERROR_OPERATION_FAILED;
525 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
527 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
528 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
531 g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
532 if (NULL == g_check_client_timer) {
533 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
536 return TTSD_ERROR_NONE;
541 SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
544 if (0 < g_list_length(g_proc_list)) {
545 iter = g_list_first(g_proc_list);
546 while (NULL != iter) {
547 g_proc_list = g_list_remove_link(g_proc_list, iter);
549 iter = g_list_first(g_proc_list);
553 ttsd_config_finalize();
555 ttsd_player_release();
557 ttsd_engine_agent_release();
561 ecore_timer_del(g_wait_timer);
563 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
566 if (g_terminate_timer) {
567 ecore_timer_del(g_terminate_timer);
568 g_terminate_timer = NULL;
569 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore terminating timer handle");
572 if (g_check_client_timer) {
573 ecore_timer_del(g_check_client_timer);
574 g_check_client_timer = NULL;
575 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
578 if (g_quit_loop_timer) {
579 ecore_timer_del(g_quit_loop_timer);
580 g_quit_loop_timer = NULL;
581 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
584 return TTSD_ERROR_NONE;
589 SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
591 ttsd_terminate_daemon(NULL);
593 return TTSD_ERROR_NONE;
597 * TTS Server Functions for Client
600 int ttsd_server_is_already_initialized(int pid, int uid, bool* is_initialized)
602 if (-1 != ttsd_data_is_client(uid))
603 *is_initialized = true;
605 *is_initialized = false;
607 SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), Uid(%d) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
608 return TTSD_ERROR_NONE;
611 int ttsd_server_initialize(int pid, int uid, tts_ipc_method_e method, bool* credential_needed)
613 SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
615 if (-1 != ttsd_data_is_client(uid)) {
616 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
617 return TTSD_ERROR_NONE;
620 if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
621 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
622 return TTSD_ERROR_OPERATION_FAILED;
625 if (true == *credential_needed) {
627 if (0 != app_manager_get_app_id(pid, &appid)) {
628 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
630 bool is_agreed = false;
631 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
632 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
635 return TTSD_ERROR_OPERATION_FAILED;
640 if (false == is_agreed) {
641 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
642 return TTSD_ERROR_PERMISSION_DENIED;
646 if (0 != ttsd_data_new_client(pid, uid)) {
647 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
648 return TTSD_ERROR_OPERATION_FAILED;
651 if (0 != ttsd_data_set_ipc_method(uid, method)) {
652 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set ipc method");
653 return TTSD_ERROR_OPERATION_FAILED;
656 if (0 != ttsd_player_create_instance(uid)) {
657 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
658 return TTSD_ERROR_OPERATION_FAILED;
662 g_min_latency = 10000.0;
667 return TTSD_ERROR_NONE;
670 static Eina_Bool __quit_ecore_loop(void *data)
672 ttsd_ipc_close_connection();
673 ttsd_network_finalize();
675 ecore_main_loop_quit();
681 static void __read_proc()
684 struct dirent *dirp = NULL;
688 if (0 < g_list_length(g_proc_list)) {
689 iter = g_list_first(g_proc_list);
690 while (NULL != iter) {
691 g_proc_list = g_list_remove_link(g_proc_list, iter);
693 iter = g_list_first(g_proc_list);
697 dp = opendir("/proc");
699 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
705 tmp = atoi(dirp->d_name);
706 if (0 >= tmp) continue;
707 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
709 } while (NULL != dirp);
715 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
721 for (i = 0; i < g_list_length(g_proc_list); i++) {
722 iter = g_list_nth(g_proc_list, i);
724 if (pid == GPOINTER_TO_INT(iter->data)) {
725 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
732 if (false == exist) {
733 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
734 ttsd_server_finalize(uid);
739 char appid[1024] = {0, };
740 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
741 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
744 if (0 < strlen(appid)) {
745 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
747 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
750 result = ttsdc_send_hello(pid, uid);
753 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
754 ttsd_server_finalize(uid);
755 } else if (-1 == result) {
756 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
764 Eina_Bool ttsd_cleanup_client(void *data)
766 SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
769 if (0 < ttsd_data_get_client_count()) {
770 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
772 if (NULL == g_quit_loop_timer) {
773 SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
774 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
776 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
777 g_check_client_timer = NULL;
781 SLOG(LOG_DEBUG, tts_tag(), "@@@");
786 void __used_voice_cb(const char* lang, int type)
788 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
789 if (0 != ttsd_engine_unload_voice(lang, type)) {
790 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
794 int ttsd_server_finalize(int uid)
796 SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
797 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);
799 app_tts_state_e state;
800 if (0 > ttsd_data_get_client_state(uid, &state)) {
801 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
804 ttsd_server_stop(uid);
805 ttsd_player_clear(uid);
807 ttsd_player_destroy_instance(uid);
809 /* Need to unload voice when used voice is unregistered */
810 if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
811 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
812 return TTSD_ERROR_OPERATION_FAILED;
815 ttsd_data_delete_client(uid);
817 /* unload engine, if ref count of client is 0 */
818 if (0 == ttsd_data_get_client_count()) {
819 if (NULL == g_quit_loop_timer) {
820 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
821 g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
823 SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
827 return TTSD_ERROR_NONE;
830 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
832 app_tts_state_e state;
833 if (0 > ttsd_data_get_client_state(uid, &state)) {
834 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
835 return TTSD_ERROR_INVALID_PARAMETER;
838 /* check valid voice */
839 char* temp_lang = NULL;
841 if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
842 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
843 if (NULL != temp_lang) {
847 return TTSD_ERROR_INVALID_VOICE;
850 if (NULL == temp_lang) {
851 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
852 return TTSD_ERROR_INVALID_VOICE;
855 speak_data_s* speak_data = NULL;
856 speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
857 if (NULL == speak_data) {
858 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
859 if (NULL != temp_lang) {
863 return TTSD_ERROR_OPERATION_FAILED;
866 speak_data->lang = strdup(lang);
867 speak_data->vctype = voice_type;
869 speak_data->speed = speed;
870 speak_data->utt_id = utt_id;
872 speak_data->text = strdup(text);
874 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);
876 /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
878 ret = ttsd_data_add_speak_data(uid, speak_data);
880 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
881 if (NULL != temp_lang) {
885 if (NULL != speak_data) {
886 if (NULL != speak_data->lang) free(speak_data->lang);
887 if (NULL != speak_data->text) free(speak_data->text);
889 speak_data->lang = NULL;
890 speak_data->text = NULL;
899 if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
900 /* Request load voice */
901 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
902 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
903 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
907 if (NULL != temp_lang) {
912 if (APP_STATE_PLAYING == state) {
913 /* check if engine use network */
914 if (ttsd_engine_agent_need_network()) {
915 if (false == ttsd_network_is_connected()) {
916 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
917 return TTSD_ERROR_OPERATION_FAILED;
921 /* Check whether tts-engine is running or not */
922 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
923 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
925 __synthesis(uid, credential);
929 return TTSD_ERROR_NONE;
932 Eina_Bool __send_interrupt_client(void *data)
934 intptr_t puid = (intptr_t)data;
937 int pid = ttsd_data_get_pid(uid);
939 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%d), pid(%d)", uid, pid);
943 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
944 /* send message to client about changing state */
945 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
947 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
953 int ttsd_server_play(int uid, const char* credential)
955 app_tts_state_e state;
956 if (0 > ttsd_data_get_client_state(uid, &state)) {
957 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
958 return TTSD_ERROR_INVALID_PARAMETER;
961 if (APP_STATE_PLAYING == state) {
962 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
963 return TTSD_ERROR_NONE;
966 /* check if engine use network */
967 if (ttsd_engine_agent_need_network()) {
968 if (false == ttsd_network_is_connected()) {
969 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
970 return TTSD_ERROR_OUT_OF_NETWORK;
974 /* check the current playback focus */
975 if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
976 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
977 ttsd_data_clear_data(uid);
978 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
981 int current_uid = ttsd_data_get_current_playing();
982 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
984 if (uid != current_uid && -1 != current_uid) {
985 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
986 /* Send interrupt message */
987 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
990 if (0 != ttsd_server_stop(current_uid)) {
991 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
994 intptr_t pcurrent_uid = (intptr_t)current_uid;
995 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
997 /* Default mode policy of interrupt is "Pause" */
999 /* Send interrupt message */
1000 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1003 if (0 != ttsd_player_pause(current_uid)) {
1004 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1008 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1010 intptr_t pcurrent_uid = (intptr_t)current_uid;
1011 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1015 /* Change current play */
1016 if (0 != ttsd_player_wait_to_play(uid)) {
1017 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%d)", uid);
1020 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1021 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1022 return TTSD_ERROR_OPERATION_FAILED;
1025 if (APP_STATE_PAUSED == state) {
1026 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1028 g_is_paused = false;
1031 if (0 != ttsd_player_resume(uid)) {
1032 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1035 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%d)", uid);
1037 if (0 != ttsd_player_play(uid)) {
1038 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_play()");
1042 /* Check whether tts-engine is running or not */
1043 clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1044 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
1045 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1047 __synthesis(uid, credential);
1050 return TTSD_ERROR_NONE;
1053 int ttsd_server_stop(int uid)
1055 app_tts_state_e state;
1056 if (0 > ttsd_data_get_client_state(uid, &state)) {
1057 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1058 return TTSD_ERROR_INVALID_PARAMETER;
1061 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1063 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1064 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
1065 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1068 ret = ttsd_engine_cancel_synthesis();
1070 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1072 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1075 if (0 != ttsd_player_stop(uid)) {
1076 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1079 ttsd_data_set_client_state(uid, APP_STATE_READY);
1082 /* Reset all data */
1083 ttsd_data_clear_data(uid);
1084 g_is_paused = false;
1086 return TTSD_ERROR_NONE;
1089 int ttsd_server_pause(int uid, int* utt_id)
1091 app_tts_state_e state;
1092 if (0 > ttsd_data_get_client_state(uid, &state)) {
1093 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1094 return TTSD_ERROR_INVALID_PARAMETER;
1097 if (APP_STATE_PLAYING != state) {
1098 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1099 return TTSD_ERROR_INVALID_STATE;
1104 *utt_id = g_utt.uttid;
1105 SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
1108 ret = ttsd_player_pause(uid);
1110 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1111 return TTSD_ERROR_OPERATION_FAILED;
1114 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1116 return TTSD_ERROR_NONE;
1119 int ttsd_server_get_support_voices(int uid, GList** voice_list)
1121 app_tts_state_e state;
1122 if (0 > ttsd_data_get_client_state(uid, &state)) {
1123 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1124 return TTSD_ERROR_INVALID_PARAMETER;
1128 int ret = ttsd_engine_get_voice_list(voice_list);
1130 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1134 SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1136 return TTSD_ERROR_NONE;
1139 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
1141 app_tts_state_e state;
1142 if (0 > ttsd_data_get_client_state(uid, &state)) {
1143 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1144 return TTSD_ERROR_INVALID_PARAMETER;
1147 /* get current voice */
1148 int ret = ttsd_engine_get_default_voice(language, voice_type);
1150 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1154 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1156 return TTSD_ERROR_NONE;
1159 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1161 app_tts_state_e state;
1162 if (0 > ttsd_data_get_client_state(uid, &state)) {
1163 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1164 return TTSD_ERROR_INVALID_PARAMETER;
1167 if (APP_STATE_READY != state) {
1168 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1169 return TTSD_ERROR_INVALID_STATE;
1172 int ret = ttsd_engine_set_private_data(key, data);
1174 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1176 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1182 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1184 app_tts_state_e state;
1185 if (0 > ttsd_data_get_client_state(uid, &state)) {
1186 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1187 return TTSD_ERROR_INVALID_PARAMETER;
1190 if (APP_STATE_READY != state) {
1191 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1192 return TTSD_ERROR_INVALID_STATE;
1195 int ret = ttsd_engine_get_private_data(key, data);
1197 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1199 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1205 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1207 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1210 ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1212 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1218 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1220 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1223 ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1225 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1231 int ttsd_server_play_pcm(int uid)
1233 app_tts_state_e state;
1234 if (0 > ttsd_data_get_client_state(uid, &state)) {
1235 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid ", uid);
1236 return TTSD_ERROR_INVALID_PARAMETER;
1239 if (APP_STATE_PLAYING == state) {
1240 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1241 return TTSD_ERROR_NONE;
1244 int current_uid = ttsd_data_get_current_playing();
1245 SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1247 if (uid != current_uid && -1 != current_uid) {
1248 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1249 /* Send interrupt message */
1250 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1253 if (0 != ttsd_server_stop(current_uid)) {
1254 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%d)", current_uid);
1257 intptr_t pcurrent_uid = (intptr_t)current_uid;
1258 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1260 /* Default mode policy of interrupt is "Pause" */
1262 /* Send interrupt message */
1263 SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1266 if (0 != ttsd_player_pause(current_uid)) {
1267 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1271 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1273 intptr_t pcurrent_uid = (intptr_t)current_uid;
1274 ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1278 /* Change current play */
1279 if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1280 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1281 return TTSD_ERROR_OPERATION_FAILED;
1284 if (APP_STATE_PAUSED == state) {
1285 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
1288 if (0 != ttsd_player_resume(uid)) {
1289 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
1292 if (0 != ttsd_player_play(uid)) {
1293 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play pcm sound : uid(%d)", uid);
1295 // Change ready state
1296 ttsd_server_stop_pcm(uid);
1298 int tmp_pid = ttsd_data_get_pid(uid);
1300 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%d)", uid);
1302 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1307 return TTSD_ERROR_NONE;
1310 int ttsd_server_stop_pcm(int uid)
1312 app_tts_state_e state;
1313 if (0 > ttsd_data_get_client_state(uid, &state)) {
1314 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1315 return TTSD_ERROR_INVALID_PARAMETER;
1318 SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1320 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1321 ttsd_data_set_client_state(uid, APP_STATE_READY);
1324 /* Reset all data */
1325 ttsd_data_clear_data(uid);
1327 ttsd_player_stop(uid);
1330 return TTSD_ERROR_NONE;
1333 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1335 SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1339 /* Synthesis is success */
1340 if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1341 if (TTSE_RESULT_EVENT_START == event) {
1342 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1343 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1344 uid, uttid, data, data_size, audio_type, rate);
1345 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1346 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1347 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1348 uid, uttid, data, data_size, audio_type, rate);
1350 /*if (TTSE_RESULT_EVENT_CONTINUE == event) SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1353 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1354 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1355 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1356 return TTSD_ERROR_INVALID_PARAMETER;
1360 sound_data_s* temp_sound_data = NULL;
1361 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1362 if (NULL == temp_sound_data) {
1363 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1364 return TTSD_ERROR_OUT_OF_MEMORY;
1367 temp_sound_data->data = NULL;
1368 temp_sound_data->rate = 0;
1369 temp_sound_data->data_size = 0;
1371 if (0 < data_size) {
1372 temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1373 if (NULL != temp_sound_data->data) {
1374 memcpy(temp_sound_data->data, data, data_size);
1375 temp_sound_data->data_size = data_size;
1376 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)",
1377 uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1379 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1382 SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1385 temp_sound_data->utt_id = uttid;
1386 temp_sound_data->event = event;
1387 temp_sound_data->audio_type = audio_type;
1388 temp_sound_data->rate = rate;
1390 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1391 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1393 if (NULL != temp_sound_data->data) {
1394 free(temp_sound_data->data);
1395 temp_sound_data->data = NULL;
1398 free(temp_sound_data);
1399 temp_sound_data = NULL;
1402 /* if (0 != ttsd_player_play(uid)) {
1403 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1405 // Change ready state
1406 ttsd_server_stop(uid);
1409 tmp_pid = ttsd_data_get_pid(uid);
1410 ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1414 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1417 SLOG(LOG_DEBUG, tts_tag(), "@@@");
1419 return TTSD_ERROR_NONE;