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