Add ecore_shutdown to shut the ecore of ttse_main
[platform/core/uifw/tts.git] / server / ttsd_server.c
1 /*
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.
12 */
13
14 #include <aul.h>
15 #include <Ecore.h>
16
17 #include "ttsd_config.h"
18 #include "ttsd_data.h"
19 #include "ttsd_dbus.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_engine_agent.h"
22 #include "ttsd_main.h"
23 #include "ttsd_network.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
26
27
28 #define CLIENT_CLEAN_UP_TIME 500
29
30
31 typedef enum {
32         TTSD_SYNTHESIS_CONTROL_DOING    = 0,
33         TTSD_SYNTHESIS_CONTROL_DONE     = 1,
34         TTSD_SYNTHESIS_CONTROL_EXPIRED  = 2
35 } ttsd_synthesis_control_e;
36
37 typedef struct {
38         int uid;
39         int uttid;
40 } utterance_t;
41
42 /* If current engine exist */
43 //static bool   g_is_engine;
44
45 /* If engine is running */
46 static ttsd_synthesis_control_e g_synth_control;
47
48 static Ecore_Timer* g_check_client_timer = NULL;
49 static Ecore_Timer* g_wait_timer = NULL;
50
51 static utterance_t g_utt;
52
53 static GList *g_proc_list = NULL;
54
55 /* Function definitions */
56 static int __synthesis(int uid, const char* credential);
57
58 static int __server_set_synth_control(ttsd_synthesis_control_e control)
59 {
60         g_synth_control = control;
61         return 0;
62 }
63
64 static ttsd_synthesis_control_e __server_get_synth_control()
65 {
66         return g_synth_control;
67 }
68
69 static Eina_Bool __wait_synthesis(void *data)
70 {
71         /* get current play */
72         char* credential = (char*)data;
73         int uid = ttsd_data_get_current_playing();
74
75         if (uid > 0) {
76                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
77                         return EINA_TRUE;
78                 } else {
79                         g_wait_timer = NULL;
80                         if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
81                                 /* Start next synthesis */
82                                 __synthesis(uid, credential);
83                         }
84                 }
85         } else {
86                 g_wait_timer = NULL;
87         }
88
89         return EINA_FALSE;
90 }
91
92 static int __synthesis(int uid, const char* credential)
93 {
94         SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS  START");
95
96         speak_data_s* speak_data = NULL;
97         if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
98                 if (NULL == speak_data) {
99                         SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
100                         return 0;
101                 }
102
103                 int pid = ttsd_data_get_pid(uid);
104                 char appid[128] = {0, };
105                 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
106                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
107                 }
108
109                 if (NULL == speak_data->lang || NULL == speak_data->text) {
110                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
111                         ttsd_server_stop(uid);
112
113                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
114
115                         if (NULL != speak_data) {
116                                 if (NULL != speak_data->lang)   free(speak_data->lang);
117                                 if (NULL != speak_data->text)   free(speak_data->text);
118
119                                 speak_data->lang = NULL;
120                                 speak_data->text = NULL;
121
122                                 free(speak_data);
123                                 speak_data = NULL;
124                         }
125
126                         return 0;
127                 }
128
129                 g_utt.uid = uid;
130                 g_utt.uttid = speak_data->utt_id;
131
132                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
133                 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
134                 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
135                 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
136                 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
137                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
138
139                 int ret = 0;
140                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
141                 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
142                 if (0 != ret) {
143                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
144
145                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
146
147                         ttsd_server_stop(uid);
148
149                         int pid = ttsd_data_get_pid(uid);
150                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
151                 } else {
152                         g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
153                 }
154
155                 if (NULL != speak_data) {
156                         if (NULL != speak_data->lang)   free(speak_data->lang);
157                         if (NULL != speak_data->text)   free(speak_data->text);
158
159                         speak_data->lang = NULL;
160                         speak_data->text = NULL;
161
162                         free(speak_data);
163                         speak_data = NULL;
164                 }
165         }
166
167         SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS  END");
168         SLOG(LOG_DEBUG, tts_tag(), "  ");
169
170         return 0;
171 }
172
173 /*
174 * TTS Server Callback Functions
175 */
176 int ttsd_send_error(ttse_error_e error, const char* msg)
177 {
178         int uid = g_utt.uid;
179         int uttid = g_utt.uttid;
180         int tmp_pid = ttsd_data_get_pid(uid);
181
182         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, msg);
183
184         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
185
186         if (0 != ttsd_player_clear(uid))
187                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
188
189         if (0 != ttsd_data_clear_data(uid))
190                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
191
192         if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
193                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
194
195         if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
196                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
197
198         if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
199                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
200
201         return 0;
202 }
203
204 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)
205 {
206         SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
207
208         int uid = g_utt.uid;
209         int uttid = g_utt.uttid;
210
211         /* Synthesis is success */
212         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
213                 if (TTSE_RESULT_EVENT_START == event) {
214                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
215                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
216                                 uid, uttid, data, data_size, audio_type, rate);
217                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
218                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
219                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
220                                 uid, uttid, data, data_size, audio_type, rate);
221                 } else {
222                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
223                 }
224
225
226                 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
227                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
228                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
229                         SLOG(LOG_DEBUG, tts_tag(), "=====");
230                         SLOG(LOG_DEBUG, tts_tag(), "  ");
231                         return TTSD_ERROR_OPERATION_FAILED;
232                 }
233
234                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
235                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
236                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
237                         SLOG(LOG_DEBUG, tts_tag(), "=====");
238                         SLOG(LOG_DEBUG, tts_tag(), "  ");
239                         return TTSD_ERROR_INVALID_PARAMETER;
240                 }
241
242                 /* add wav data */
243                 sound_data_s* temp_sound_data = NULL;
244                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
245                 if (NULL == temp_sound_data) {
246                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
247                         return TTSD_ERROR_OUT_OF_MEMORY;
248                 }
249
250                 temp_sound_data->data = NULL;
251                 temp_sound_data->rate = 0;
252                 temp_sound_data->data_size = 0;
253
254                 if (0 < data_size) {
255                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
256                         if (NULL != temp_sound_data->data) {
257                                 memcpy(temp_sound_data->data, data, data_size);
258                                 temp_sound_data->data_size = data_size;
259                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
260                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
261                         } else {
262                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
263                         }
264                 } else {
265                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
266                 }
267
268                 temp_sound_data->utt_id = uttid;
269                 temp_sound_data->event = event;
270                 temp_sound_data->audio_type = audio_type;
271                 temp_sound_data->rate = rate;
272
273                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
274                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
275                 }
276
277                 if (event == TTSE_RESULT_EVENT_FINISH) {
278                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
279                 }
280
281                 if (0 != ttsd_player_play(uid)) {
282                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
283
284                         /* Change ready state */
285                         ttsd_server_stop(uid);
286
287                         int tmp_pid;
288                         tmp_pid = ttsd_data_get_pid(uid);
289                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
290                 }
291         } else {
292                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
293                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
294         }
295
296
297         /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
298         SLOG(LOG_DEBUG, tts_tag(), "  ");*/
299
300         return TTSD_ERROR_NONE;
301 }
302
303 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
304 {
305         /* clear client data */
306         ttsd_data_clear_data(uid);
307         ttsd_data_set_client_state(uid, APP_STATE_READY);
308
309         /* send message */
310         if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
311                 /* remove client */
312                 ttsd_data_delete_client(uid);
313         }
314
315         return true;
316 }
317
318 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
319 {
320         switch (type) {
321         case TTS_CONFIG_TYPE_ENGINE:
322         {
323                 /* TODO - Determine the policy when engine process get engine changed cb */
324                 if (NULL == str_param) {
325                         SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
326                         return;
327                 }
328
329                 int ret = 0;
330                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
331                         SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
332                         ret = ttsd_engine_agent_unload_current_engine();
333                         if (0 != ret) {
334                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
335                         }
336
337                         ret = ttsd_engine_agent_load_current_engine();
338                         if (0 != ret) {
339                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
340                         }
341                         return;
342                 }
343
344                 /* stop all player */ 
345                 ttsd_player_all_stop();
346
347                 /* send interrupt message to  all clients */
348                 ttsd_data_foreach_clients(__get_client_cb, NULL);
349
350                 ttsd_engine_cancel_synthesis();
351
352                 break;
353         }
354
355         case TTS_CONFIG_TYPE_VOICE:
356         {
357                 if (NULL == str_param) {
358                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
359                         return;
360                 }
361
362                 char* out_lang = NULL;
363                 int out_type;
364                 int ret = -1;
365
366                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
367                         SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
368                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
369                         if (0 != ret)
370                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
371                 } else {
372                         /* Current language is not available */
373                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
374                 }
375                 if (NULL != out_lang) {
376                         free(out_lang);
377                         out_lang = NULL;
378                 }
379                 break;
380         }
381
382         case TTS_CONFIG_TYPE_SPEED:
383         {
384                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
385                         /* set default speed */
386                         int ret = 0;
387                         ret = ttsd_engine_agent_set_default_speed(int_param);
388                         if (0 != ret) {
389                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
390                         }
391                 }
392                 break;
393         }
394
395         case TTS_CONFIG_TYPE_PITCH:
396         {
397                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
398                         /* set default speed */
399                         int ret = 0;
400                         ret = ttsd_engine_agent_set_default_pitch(int_param);
401                         if (0 != ret) {
402                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
403                         }
404                 }
405                 break;
406         }
407         }
408
409         return;
410 }
411
412 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
413 {
414         SLOG(LOG_INFO, tts_tag(), "=== Start to terminate client [%d] ===", uid);
415         ttsd_server_finalize(uid);
416         return true;
417 }
418
419 Eina_Bool ttsd_terminate_daemon(void *data)
420 {
421         ttsd_data_foreach_clients(__terminate_client, NULL);
422         return EINA_FALSE;
423 }
424
425 void __screen_reader_changed_cb(bool value)
426 {
427         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
428                 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
429                 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
430         } else {
431                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
432         }
433         return;
434 }
435
436 /*
437 * Server APIs
438 */
439 int ttsd_initialize(ttse_request_callback_s *callback)
440 {
441         SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
442
443         if (ttsd_config_initialize(__config_changed_cb)) {
444                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
445         }
446
447         /* player init */
448         if (ttsd_player_init()) {
449                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
450                 return TTSD_ERROR_OPERATION_FAILED;
451         }
452
453         /* Engine Agent initialize */
454         if (0 != ttsd_engine_agent_init()) {
455                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
456                 return TTSD_ERROR_OPERATION_FAILED;
457         }
458
459         /* set current engine */
460         //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
461         //      SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
462         //      g_is_engine = false;
463         //} else
464         //      g_is_engine = true;
465
466         if (0 != ttsd_engine_agent_load_current_engine(callback)) {
467                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
468                 return TTSD_ERROR_OPERATION_FAILED;
469         }
470
471         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
472
473         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
474                 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
475         }
476
477         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
478         if (NULL == g_check_client_timer) {
479                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
480         }
481
482         return TTSD_ERROR_NONE;
483 }
484
485 int ttsd_finalize()
486 {
487         SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
488
489         GList *iter = NULL;
490         if (0 < g_list_length(g_proc_list)) {
491                 iter = g_list_first(g_proc_list);
492                 while (NULL != iter) {
493                         g_proc_list = g_list_remove_link(g_proc_list, iter);
494                         g_list_free(iter);
495                         iter = g_list_first(g_proc_list);
496                 }
497         }
498
499         ttsd_config_finalize();
500
501         ttsd_player_release();
502
503         ttsd_engine_agent_release();
504
505         if (NULL != g_check_client_timer) {
506                 ecore_timer_del(g_check_client_timer);
507                 g_check_client_timer = NULL;
508
509                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
510         }
511
512         return TTSD_ERROR_NONE;
513 }
514
515 /*
516 * TTS Server Functions for Client
517 */
518
519 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
520 {
521         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
522
523         if (-1 != ttsd_data_is_client(uid)) {
524                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
525                 return TTSD_ERROR_NONE;
526         }
527
528         if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
529                 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
530                 return TTSD_ERROR_OPERATION_FAILED;
531         }
532         if (0 != ttsd_data_new_client(pid, uid)) {
533                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
534                 return TTSD_ERROR_OPERATION_FAILED;
535         }
536
537         if (0 != ttsd_player_create_instance(uid)) {
538                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
539                 return TTSD_ERROR_OPERATION_FAILED;
540         }
541
542         return TTSD_ERROR_NONE;
543 }
544
545 static Eina_Bool __quit_ecore_loop(void *data)
546 {
547         ttsd_dbus_close_connection();
548         ttsd_network_finalize();
549         ttsd_finalize();
550         ecore_main_loop_quit();
551         ecore_shutdown();
552
553         return EINA_FALSE;
554 }
555
556
557 static void __read_proc()
558 {
559         DIR *dp = NULL;
560         struct dirent *dirp = NULL;
561         int tmp;
562
563         GList *iter = NULL;
564         if (0 < g_list_length(g_proc_list)) {
565                 iter = g_list_first(g_proc_list);
566                 while (NULL != iter) {
567                         g_proc_list = g_list_remove_link(g_proc_list, iter);
568                         g_list_free(iter);
569                         iter = g_list_first(g_proc_list);
570                 }
571         }
572
573         dp = opendir("/proc");
574         if (NULL == dp) {
575                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
576         } else {
577                 do {
578                         dirp = readdir(dp);
579
580                         if (NULL != dirp) {
581                                 tmp = atoi(dirp->d_name);
582                                 if (0 >= tmp)   continue;
583                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
584                         }
585                 } while (NULL != dirp);
586                 closedir(dp);
587         }
588         return;
589 }
590
591 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
592 {
593         bool exist = false;
594         int i = 0;
595
596         GList *iter = NULL;
597         for (i = 0; i < g_list_length(g_proc_list); i++) {
598                 iter = g_list_nth(g_proc_list, i);
599                 if (NULL != iter) {
600                         if (pid == GPOINTER_TO_INT(iter->data)) {
601                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
602                                 exist = true;
603                                 break;
604                         }
605                 }
606         }
607
608         if (false == exist) {
609                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
610                 ttsd_server_finalize(uid);
611         }
612
613         return true;
614 #if 0
615         char appid[128] = {0, };
616         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
617                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
618         }
619
620         if (0 < strlen(appid)) {
621                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
622         } else {
623                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
624
625                 int result = 1;
626                 result = ttsdc_send_hello(pid, uid);
627
628                 if (0 == result) {
629                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
630                         ttsd_server_finalize(uid);
631                 } else if (-1 == result) {
632                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
633                 }
634         }
635         return true;
636 #endif
637 }
638
639
640 Eina_Bool ttsd_cleanup_client(void *data)
641 {
642         SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
643         __read_proc();
644
645         if (0 < ttsd_data_get_client_count()) {
646                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
647         } else {
648                 ecore_timer_add(0, __quit_ecore_loop, NULL);
649         }
650
651         SLOG(LOG_DEBUG, tts_tag(), "=====");
652         SLOG(LOG_DEBUG, tts_tag(), "  ");
653
654         return EINA_TRUE;
655 }
656
657 void __used_voice_cb(const char* lang, int type)
658 {
659         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
660         if (0 != ttsd_engine_unload_voice(lang, type)) {
661                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
662         }
663 }
664
665 int ttsd_server_finalize(int uid)
666 {
667         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
668
669         app_state_e state;
670         if (0 > ttsd_data_get_client_state(uid, &state)) {
671                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
672         }
673
674         ttsd_server_stop(uid);
675         ttsd_player_stop(uid);
676
677         ttsd_player_destroy_instance(uid);
678
679         /* Need to unload voice when used voice is unregistered */
680         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
681                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
682                 return TTSD_ERROR_OPERATION_FAILED;
683         }
684
685         ttsd_data_delete_client(uid);
686
687         /* unload engine, if ref count of client is 0 */
688         if (0 == ttsd_data_get_client_count()) {
689                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
690                 ecore_timer_add(0, __quit_ecore_loop, NULL);
691         }
692
693         return TTSD_ERROR_NONE;
694 }
695
696 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
697 {
698         app_state_e state;
699         if (0 > ttsd_data_get_client_state(uid, &state)) {
700                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
701                 return TTSD_ERROR_INVALID_PARAMETER;
702         }
703
704         /* check valid voice */
705         char* temp_lang = NULL;
706         int temp_type;
707         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
708                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
709                 if (NULL != temp_lang) {
710                         free(temp_lang);
711                         temp_lang = NULL;
712                 }
713                 return TTSD_ERROR_INVALID_VOICE;
714         }
715
716         if (NULL == temp_lang) {
717                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
718                 return TTSD_ERROR_INVALID_VOICE;
719         }
720
721         speak_data_s* speak_data = NULL;
722         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
723         if (NULL == speak_data) {
724                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
725                 if (NULL != temp_lang) {
726                         free(temp_lang);
727                         temp_lang = NULL;
728                 }
729                 return TTSD_ERROR_OPERATION_FAILED;
730         }
731
732         speak_data->lang = strdup(lang);
733         speak_data->vctype = voice_type;
734
735         speak_data->speed = speed;
736         speak_data->utt_id = utt_id;
737
738         speak_data->text = strdup(text);
739
740         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);
741
742         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
743         int ret = -1;
744         ret = ttsd_data_add_speak_data(uid, speak_data);
745         if (0 != ret) {
746                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
747                 if (NULL != temp_lang) {
748                         free(temp_lang);
749                         temp_lang = NULL;
750                 }
751                 if (NULL != speak_data) {
752                         if (NULL != speak_data->lang)   free(speak_data->lang);
753                         if (NULL != speak_data->text)   free(speak_data->text);
754
755                         speak_data->lang = NULL;
756                         speak_data->text = NULL;
757
758                         free(speak_data);
759                         speak_data = NULL;
760                 }
761
762                 return ret;
763         }
764
765         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
766                 /* Request load voice */
767                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
768                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
769                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
770                 }
771         }
772
773         if (NULL != temp_lang) {
774                 free(temp_lang);
775                 temp_lang = NULL;
776         }
777
778         if (APP_STATE_PLAYING == state) {
779                 /* check if engine use network */
780                 if (ttsd_engine_agent_need_network()) {
781                         if (false == ttsd_network_is_connected()) {
782                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
783                                 return TTSD_ERROR_OPERATION_FAILED;
784                         }
785                 }
786
787                 /* Check whether tts-engine is running or not */
788                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
789                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
790                 } else {
791                         __synthesis(uid, credential);
792                 }
793         }
794
795         return TTSD_ERROR_NONE;
796 }
797
798 Eina_Bool __send_interrupt_client(void *data)
799 {
800         intptr_t puid = (intptr_t)data;
801         int uid = (int)puid;
802
803         int pid = ttsd_data_get_pid(uid);
804
805         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
806                 /* send message to client about changing state */
807                 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
808         } else {
809                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
810         }
811
812         return EINA_FALSE;
813 }
814
815 int ttsd_server_play(int uid, const char* credential)
816 {
817         app_state_e state;
818         if (0 > ttsd_data_get_client_state(uid, &state)) {
819                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
820                 return TTSD_ERROR_INVALID_PARAMETER;
821         }
822
823         if (APP_STATE_PLAYING == state) {
824                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
825                 return TTSD_ERROR_NONE;
826         }
827
828         /* check if engine use network */
829         if (ttsd_engine_agent_need_network()) {
830                 if (false == ttsd_network_is_connected()) {
831                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
832                         return TTSD_ERROR_OUT_OF_NETWORK;
833                 }
834         }
835
836         int current_uid = ttsd_data_get_current_playing();
837         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
838
839         if (uid != current_uid && -1 != current_uid) {
840                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
841                         /* Send interrupt message */
842                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
843
844                         /* pause player */
845                         if (0 != ttsd_server_stop(current_uid)) {
846                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
847                         }
848                         if (0 != ttsd_player_stop(current_uid)) {
849                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
850                         }
851
852                         intptr_t pcurrent_uid = (intptr_t)current_uid;
853                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
854                 } else {
855                         /* Default mode policy of interrupt is "Pause" */
856
857                         /* Send interrupt message */
858                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
859
860                         /* pause player */
861                         if (0 != ttsd_player_pause(current_uid)) {
862                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
863                         }
864
865                         /* change state */
866                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
867
868                         intptr_t pcurrent_uid = (intptr_t)current_uid;
869                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
870                 }
871         }
872
873         /* Change current play */
874         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
875                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
876                 return TTSD_ERROR_OPERATION_FAILED;
877         }
878
879         if (APP_STATE_PAUSED == state) {
880                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
881
882                 /* Resume player */
883                 if (0 != ttsd_player_resume(uid)) {
884                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
885                 }
886         }
887
888         /* Check whether tts-engine is running or not */
889         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
890                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
891         } else {
892                 __synthesis(uid, credential);
893         }
894
895         return TTSD_ERROR_NONE;
896 }
897
898 int ttsd_server_stop(int uid)
899 {
900         app_state_e state;
901         if (0 > ttsd_data_get_client_state(uid, &state)) {
902                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
903                 return TTSD_ERROR_INVALID_PARAMETER;
904         }
905
906         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
907
908         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
909                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
910                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
911
912                         int ret = 0;
913                         ret = ttsd_engine_cancel_synthesis();
914                         if (0 != ret)
915                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
916                 }
917
918                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
919
920                 if (0 != ttsd_player_clear(uid))
921                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
922
923                 ttsd_data_set_client_state(uid, APP_STATE_READY);
924         }
925
926         /* Reset all data */
927         ttsd_data_clear_data(uid);
928
929         return TTSD_ERROR_NONE;
930 }
931
932 int ttsd_server_pause(int uid, int* utt_id)
933 {
934         app_state_e state;
935         if (0 > ttsd_data_get_client_state(uid, &state)) {
936                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
937                 return TTSD_ERROR_INVALID_PARAMETER;
938         }
939
940         if (APP_STATE_PLAYING != state) {
941                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
942                 return TTSD_ERROR_INVALID_STATE;
943         }
944
945         *utt_id = g_utt.uttid;
946         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
947
948         int ret = 0;
949         ret = ttsd_player_pause(uid);
950         if (0 != ret) {
951                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
952                 return TTSD_ERROR_OPERATION_FAILED;
953         }
954
955         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
956
957         return TTSD_ERROR_NONE;
958 }
959
960 int ttsd_server_get_support_voices(int uid, GList** voice_list)
961 {
962         app_state_e state;
963         if (0 > ttsd_data_get_client_state(uid, &state)) {
964                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
965                 return TTSD_ERROR_INVALID_PARAMETER;
966         }
967
968         /* get voice list*/
969         int ret = ttsd_engine_get_voice_list(voice_list);
970         if (0 != ret) {
971                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
972                 return ret;
973         }
974
975         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
976
977         return TTSD_ERROR_NONE;
978 }
979
980 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
981 {
982         app_state_e state;
983         if (0 > ttsd_data_get_client_state(uid, &state)) {
984                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
985                 return TTSD_ERROR_INVALID_PARAMETER;
986         }
987
988         /* get current voice */
989         int ret = ttsd_engine_get_default_voice(language, voice_type);
990         if (0 != ret) {
991                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
992                 return ret;
993         }
994
995         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
996
997         return TTSD_ERROR_NONE;
998 }
999
1000 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
1001 {
1002         app_state_e state;
1003         if (0 > ttsd_data_get_client_state(uid, &state)) {
1004                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1005                 return TTSD_ERROR_INVALID_PARAMETER;
1006         }
1007
1008         if (APP_STATE_READY != state) {
1009                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1010                 return TTSD_ERROR_INVALID_STATE;
1011         }
1012
1013         int ret = ttsd_engine_set_private_data(key, data);
1014         if (0 != ret) {
1015                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1016         } else {
1017                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1018         }
1019
1020         return ret;
1021 }
1022
1023 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1024 {
1025         app_state_e state;
1026         if (0 > ttsd_data_get_client_state(uid, &state)) {
1027                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1028                 return TTSD_ERROR_INVALID_PARAMETER;
1029         }
1030
1031         if (APP_STATE_READY != state) {
1032                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1033                 return TTSD_ERROR_INVALID_STATE;
1034         }
1035
1036         int ret = ttsd_engine_get_private_data(key, data);
1037         if (0 != ret) {
1038                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1039         } else {
1040                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1041         }
1042
1043         return ret;
1044 }
1045
1046 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1047 {
1048         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1049
1050         int ret = 0;
1051         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1052         if (0 != ret) {
1053                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1054         }
1055
1056         return ret;
1057 }
1058
1059 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1060 {
1061         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1062
1063         int ret = 0;
1064         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1065         if (0 != ret) {
1066                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1067         }
1068
1069         return ret;
1070 }
1071
1072 int ttsd_server_play_pcm(int uid)
1073 {
1074         app_state_e state;
1075         if (0 > ttsd_data_get_client_state(uid, &state)) {
1076                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
1077                 return TTSD_ERROR_INVALID_PARAMETER;
1078         }
1079
1080         if (APP_STATE_PLAYING == state) {
1081                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1082                 return TTSD_ERROR_NONE;
1083         }
1084
1085         int current_uid = ttsd_data_get_current_playing();
1086         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1087
1088         if (uid != current_uid && -1 != current_uid) {
1089                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1090                         /* Send interrupt message */
1091                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1092
1093                         /* pause player */
1094                         if (0 != ttsd_server_stop(current_uid)) {
1095                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1096                         }
1097                         if (0 != ttsd_player_stop(current_uid)) {
1098                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1099                         }
1100
1101                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1102                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1103                 } else {
1104                         /* Default mode policy of interrupt is "Pause" */
1105
1106                         /* Send interrupt message */
1107                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1108
1109                         /* pause player */
1110                         if (0 != ttsd_player_pause(current_uid)) {
1111                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1112                         }
1113
1114                         /* change state */
1115                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1116
1117                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1118                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1119                 }
1120         }
1121
1122         /* Change current play */
1123         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1124                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1125                 return TTSD_ERROR_OPERATION_FAILED;
1126         }
1127
1128         return TTSD_ERROR_NONE;
1129 }
1130
1131 int ttsd_server_stop_pcm(int uid)
1132 {
1133         app_state_e state;
1134         if (0 > ttsd_data_get_client_state(uid, &state)) {
1135                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1136                 return TTSD_ERROR_INVALID_PARAMETER;
1137         }
1138
1139         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1140
1141         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1142                 if (0 != ttsd_player_clear(uid))
1143                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1144
1145                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1146         }
1147
1148         /* Reset all data */
1149         ttsd_data_clear_data(uid);
1150
1151         return TTSD_ERROR_NONE;
1152 }
1153
1154 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1155 {
1156         SLOG(LOG_DEBUG, tts_tag(), "===== ADD PCM");
1157
1158         int uttid = -1;
1159
1160         /* Synthesis is success */
1161         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1162                 if (TTSE_RESULT_EVENT_START == event) {
1163                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1164                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1165                                 uid, uttid, data, data_size, audio_type, rate);
1166                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1167                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1168                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1169                                 uid, uttid, data, data_size, audio_type, rate);
1170                 } else {
1171                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1172                 }
1173
1174                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1175                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1176                         SLOG(LOG_DEBUG, tts_tag(), "=====");
1177                         SLOG(LOG_DEBUG, tts_tag(), "  ");
1178                         return TTSD_ERROR_INVALID_PARAMETER;
1179                 }
1180
1181                 /* add wav data */
1182                 sound_data_s* temp_sound_data = NULL;
1183                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1184                 if (NULL == temp_sound_data) {
1185                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1186                         return TTSD_ERROR_OUT_OF_MEMORY;
1187                 }
1188
1189                 temp_sound_data->data = NULL;
1190                 temp_sound_data->rate = 0;
1191                 temp_sound_data->data_size = 0;
1192
1193                 if (0 < data_size) {
1194                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1195                         if (NULL != temp_sound_data->data) {
1196                                 memcpy(temp_sound_data->data, data, data_size);
1197                                 temp_sound_data->data_size = data_size;
1198                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
1199                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1200                         } else {
1201                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1202                         }
1203                 } else {
1204                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1205                 }
1206
1207                 temp_sound_data->utt_id = uttid;
1208                 temp_sound_data->event = event;
1209                 temp_sound_data->audio_type = audio_type;
1210                 temp_sound_data->rate = rate;
1211
1212                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1213                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1214
1215                         if (NULL != temp_sound_data->data) {
1216                                 free(temp_sound_data->data);
1217                                 temp_sound_data->data = NULL;
1218                         }
1219
1220                         free(temp_sound_data);
1221                         temp_sound_data = NULL;
1222                 }
1223
1224                 if (0 != ttsd_player_play(uid)) {
1225                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1226
1227                         /* Change ready state */
1228                         ttsd_server_stop(uid);
1229
1230                         int tmp_pid;
1231                         tmp_pid = ttsd_data_get_pid(uid);
1232                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1233                 }
1234         } else {
1235                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1236         }
1237
1238         SLOG(LOG_DEBUG, tts_tag(), "=====");
1239
1240         return TTSD_ERROR_NONE;
1241 }