Remove null check from ttse_send_error
[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, (NULL == msg ? "NULL" : 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
549         return EINA_FALSE;
550 }
551
552
553 static void __read_proc()
554 {
555         DIR *dp = NULL;
556         struct dirent *dirp = NULL;
557         int tmp;
558
559         GList *iter = NULL;
560         if (0 < g_list_length(g_proc_list)) {
561                 iter = g_list_first(g_proc_list);
562                 while (NULL != iter) {
563                         g_proc_list = g_list_remove_link(g_proc_list, iter);
564                         g_list_free(iter);
565                         iter = g_list_first(g_proc_list);
566                 }
567         }
568
569         dp = opendir("/proc");
570         if (NULL == dp) {
571                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
572         } else {
573                 do {
574                         dirp = readdir(dp);
575
576                         if (NULL != dirp) {
577                                 tmp = atoi(dirp->d_name);
578                                 if (0 >= tmp)   continue;
579                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
580                         }
581                 } while (NULL != dirp);
582                 closedir(dp);
583         }
584         return;
585 }
586
587 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
588 {
589         bool exist = false;
590         int i = 0;
591
592         GList *iter = NULL;
593         for (i = 0; i < g_list_length(g_proc_list); i++) {
594                 iter = g_list_nth(g_proc_list, i);
595                 if (NULL != iter) {
596                         if (pid == GPOINTER_TO_INT(iter->data)) {
597                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
598                                 exist = true;
599                                 break;
600                         }
601                 }
602         }
603
604         if (false == exist) {
605                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
606                 ttsd_server_finalize(uid);
607         }
608
609         return true;
610 #if 0
611         char appid[128] = {0, };
612         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
613                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
614         }
615
616         if (0 < strlen(appid)) {
617                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
618         } else {
619                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
620
621                 int result = 1;
622                 result = ttsdc_send_hello(pid, uid);
623
624                 if (0 == result) {
625                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
626                         ttsd_server_finalize(uid);
627                 } else if (-1 == result) {
628                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
629                 }
630         }
631         return true;
632 #endif
633 }
634
635
636 Eina_Bool ttsd_cleanup_client(void *data)
637 {
638         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
639         __read_proc();
640
641         if (0 < ttsd_data_get_client_count()) {
642                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
643         } else {
644                 ecore_timer_add(0, __quit_ecore_loop, NULL);
645         }
646
647         SLOG(LOG_DEBUG, tts_tag(), "@@@");
648
649         return EINA_TRUE;
650 }
651
652 void __used_voice_cb(const char* lang, int type)
653 {
654         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
655         if (0 != ttsd_engine_unload_voice(lang, type)) {
656                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
657         }
658 }
659
660 int ttsd_server_finalize(int uid)
661 {
662         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
663
664         app_state_e state;
665         if (0 > ttsd_data_get_client_state(uid, &state)) {
666                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
667         }
668
669         ttsd_server_stop(uid);
670         ttsd_player_stop(uid);
671
672         ttsd_player_destroy_instance(uid);
673
674         /* Need to unload voice when used voice is unregistered */
675         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
676                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
677                 return TTSD_ERROR_OPERATION_FAILED;
678         }
679
680         ttsd_data_delete_client(uid);
681
682         /* unload engine, if ref count of client is 0 */
683         if (0 == ttsd_data_get_client_count()) {
684                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
685                 ecore_timer_add(0, __quit_ecore_loop, NULL);
686         }
687
688         return TTSD_ERROR_NONE;
689 }
690
691 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
692 {
693         app_state_e state;
694         if (0 > ttsd_data_get_client_state(uid, &state)) {
695                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
696                 return TTSD_ERROR_INVALID_PARAMETER;
697         }
698
699         /* check valid voice */
700         char* temp_lang = NULL;
701         int temp_type;
702         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
703                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
704                 if (NULL != temp_lang) {
705                         free(temp_lang);
706                         temp_lang = NULL;
707                 }
708                 return TTSD_ERROR_INVALID_VOICE;
709         }
710
711         if (NULL == temp_lang) {
712                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
713                 return TTSD_ERROR_INVALID_VOICE;
714         }
715
716         speak_data_s* speak_data = NULL;
717         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
718         if (NULL == speak_data) {
719                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
720                 if (NULL != temp_lang) {
721                         free(temp_lang);
722                         temp_lang = NULL;
723                 }
724                 return TTSD_ERROR_OPERATION_FAILED;
725         }
726
727         speak_data->lang = strdup(lang);
728         speak_data->vctype = voice_type;
729
730         speak_data->speed = speed;
731         speak_data->utt_id = utt_id;
732
733         speak_data->text = strdup(text);
734
735         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);
736
737         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
738         int ret = -1;
739         ret = ttsd_data_add_speak_data(uid, speak_data);
740         if (0 != ret) {
741                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
742                 if (NULL != temp_lang) {
743                         free(temp_lang);
744                         temp_lang = NULL;
745                 }
746                 if (NULL != speak_data) {
747                         if (NULL != speak_data->lang)   free(speak_data->lang);
748                         if (NULL != speak_data->text)   free(speak_data->text);
749
750                         speak_data->lang = NULL;
751                         speak_data->text = NULL;
752
753                         free(speak_data);
754                         speak_data = NULL;
755                 }
756
757                 return ret;
758         }
759
760         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
761                 /* Request load voice */
762                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
763                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
764                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
765                 }
766         }
767
768         if (NULL != temp_lang) {
769                 free(temp_lang);
770                 temp_lang = NULL;
771         }
772
773         if (APP_STATE_PLAYING == state) {
774                 /* check if engine use network */
775                 if (ttsd_engine_agent_need_network()) {
776                         if (false == ttsd_network_is_connected()) {
777                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
778                                 return TTSD_ERROR_OPERATION_FAILED;
779                         }
780                 }
781
782                 /* Check whether tts-engine is running or not */
783                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
784                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
785                 } else {
786                         __synthesis(uid, credential);
787                 }
788         }
789
790         return TTSD_ERROR_NONE;
791 }
792
793 Eina_Bool __send_interrupt_client(void *data)
794 {
795         intptr_t puid = (intptr_t)data;
796         int uid = (int)puid;
797
798         int pid = ttsd_data_get_pid(uid);
799
800         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
801                 /* send message to client about changing state */
802                 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
803         } else {
804                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
805         }
806
807         return EINA_FALSE;
808 }
809
810 int ttsd_server_play(int uid, const char* credential)
811 {
812         app_state_e state;
813         if (0 > ttsd_data_get_client_state(uid, &state)) {
814                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
815                 return TTSD_ERROR_INVALID_PARAMETER;
816         }
817
818         if (APP_STATE_PLAYING == state) {
819                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
820                 return TTSD_ERROR_NONE;
821         }
822
823         /* check if engine use network */
824         if (ttsd_engine_agent_need_network()) {
825                 if (false == ttsd_network_is_connected()) {
826                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
827                         return TTSD_ERROR_OUT_OF_NETWORK;
828                 }
829         }
830
831         int current_uid = ttsd_data_get_current_playing();
832         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
833
834         if (uid != current_uid && -1 != current_uid) {
835                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
836                         /* Send interrupt message */
837                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
838
839                         /* pause player */
840                         if (0 != ttsd_server_stop(current_uid)) {
841                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
842                         }
843                         if (0 != ttsd_player_stop(current_uid)) {
844                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
845                         }
846
847                         intptr_t pcurrent_uid = (intptr_t)current_uid;
848                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
849                 } else {
850                         /* Default mode policy of interrupt is "Pause" */
851
852                         /* Send interrupt message */
853                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
854
855                         /* pause player */
856                         if (0 != ttsd_player_pause(current_uid)) {
857                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
858                         }
859
860                         /* change state */
861                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
862
863                         intptr_t pcurrent_uid = (intptr_t)current_uid;
864                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
865                 }
866         }
867
868         /* Change current play */
869         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
870                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
871                 return TTSD_ERROR_OPERATION_FAILED;
872         }
873
874         if (APP_STATE_PAUSED == state) {
875                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
876
877                 /* Resume player */
878                 if (0 != ttsd_player_resume(uid)) {
879                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
880                 }
881         }
882
883         /* Check whether tts-engine is running or not */
884         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
885                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
886         } else {
887                 __synthesis(uid, credential);
888         }
889
890         return TTSD_ERROR_NONE;
891 }
892
893 int ttsd_server_stop(int uid)
894 {
895         app_state_e state;
896         if (0 > ttsd_data_get_client_state(uid, &state)) {
897                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
898                 return TTSD_ERROR_INVALID_PARAMETER;
899         }
900
901         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
902
903         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
904                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control() && uid == ttsd_data_get_current_playing()) {
905                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
906
907                         int ret = 0;
908                         ret = ttsd_engine_cancel_synthesis();
909                         if (0 != ret)
910                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
911                 }
912
913                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
914
915                 if (0 != ttsd_player_clear(uid))
916                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
917
918                 ttsd_data_set_client_state(uid, APP_STATE_READY);
919         }
920
921         /* Reset all data */
922         ttsd_data_clear_data(uid);
923
924         return TTSD_ERROR_NONE;
925 }
926
927 int ttsd_server_pause(int uid, int* utt_id)
928 {
929         app_state_e state;
930         if (0 > ttsd_data_get_client_state(uid, &state)) {
931                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
932                 return TTSD_ERROR_INVALID_PARAMETER;
933         }
934
935         if (APP_STATE_PLAYING != state) {
936                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
937                 return TTSD_ERROR_INVALID_STATE;
938         }
939
940         *utt_id = g_utt.uttid;
941         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%d), g_uid(%d), utt_id(%d)", uid, g_utt.uid, *utt_id);
942
943         int ret = 0;
944         ret = ttsd_player_pause(uid);
945         if (0 != ret) {
946                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
947                 return TTSD_ERROR_OPERATION_FAILED;
948         }
949
950         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
951
952         return TTSD_ERROR_NONE;
953 }
954
955 int ttsd_server_get_support_voices(int uid, GList** voice_list)
956 {
957         app_state_e state;
958         if (0 > ttsd_data_get_client_state(uid, &state)) {
959                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
960                 return TTSD_ERROR_INVALID_PARAMETER;
961         }
962
963         /* get voice list*/
964         int ret = ttsd_engine_get_voice_list(voice_list);
965         if (0 != ret) {
966                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
967                 return ret;
968         }
969
970         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
971
972         return TTSD_ERROR_NONE;
973 }
974
975 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
976 {
977         app_state_e state;
978         if (0 > ttsd_data_get_client_state(uid, &state)) {
979                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
980                 return TTSD_ERROR_INVALID_PARAMETER;
981         }
982
983         /* get current voice */
984         int ret = ttsd_engine_get_default_voice(language, voice_type);
985         if (0 != ret) {
986                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
987                 return ret;
988         }
989
990         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
991
992         return TTSD_ERROR_NONE;
993 }
994
995 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
996 {
997         app_state_e state;
998         if (0 > ttsd_data_get_client_state(uid, &state)) {
999                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1000                 return TTSD_ERROR_INVALID_PARAMETER;
1001         }
1002
1003         if (APP_STATE_READY != state) {
1004                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1005                 return TTSD_ERROR_INVALID_STATE;
1006         }
1007
1008         int ret = ttsd_engine_set_private_data(key, data);
1009         if (0 != ret) {
1010                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1011         } else {
1012                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1013         }
1014
1015         return ret;
1016 }
1017
1018 int ttsd_server_get_private_data(int uid, const char* key, char** data)
1019 {
1020         app_state_e state;
1021         if (0 > ttsd_data_get_client_state(uid, &state)) {
1022                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
1023                 return TTSD_ERROR_INVALID_PARAMETER;
1024         }
1025
1026         if (APP_STATE_READY != state) {
1027                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1028                 return TTSD_ERROR_INVALID_STATE;
1029         }
1030
1031         int ret = ttsd_engine_get_private_data(key, data);
1032         if (0 != ret) {
1033                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1034         } else {
1035                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1036         }
1037
1038         return ret;
1039 }
1040
1041 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1042 {
1043         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1044
1045         int ret = 0;
1046         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1047         if (0 != ret) {
1048                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1049         }
1050
1051         return ret;
1052 }
1053
1054 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1055 {
1056         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1057
1058         int ret = 0;
1059         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1060         if (0 != ret) {
1061                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1062         }
1063
1064         return ret;
1065 }
1066
1067 int ttsd_server_play_pcm(int uid)
1068 {
1069         app_state_e state;
1070         if (0 > ttsd_data_get_client_state(uid, &state)) {
1071                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
1072                 return TTSD_ERROR_INVALID_PARAMETER;
1073         }
1074
1075         if (APP_STATE_PLAYING == state) {
1076                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
1077                 return TTSD_ERROR_NONE;
1078         }
1079
1080         int current_uid = ttsd_data_get_current_playing();
1081         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
1082
1083         if (uid != current_uid && -1 != current_uid) {
1084                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
1085                         /* Send interrupt message */
1086                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
1087
1088                         /* pause player */
1089                         if (0 != ttsd_server_stop(current_uid)) {
1090                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
1091                         }
1092                         if (0 != ttsd_player_stop(current_uid)) {
1093                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
1094                         }
1095
1096                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1097                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1098                 } else {
1099                         /* Default mode policy of interrupt is "Pause" */
1100
1101                         /* Send interrupt message */
1102                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
1103
1104                         /* pause player */
1105                         if (0 != ttsd_player_pause(current_uid)) {
1106                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
1107                         }
1108
1109                         /* change state */
1110                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
1111
1112                         intptr_t pcurrent_uid = (intptr_t)current_uid;
1113                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
1114                 }
1115         }
1116
1117         /* Change current play */
1118         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1119                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
1120                 return TTSD_ERROR_OPERATION_FAILED;
1121         }
1122
1123         return TTSD_ERROR_NONE;
1124 }
1125
1126 int ttsd_server_stop_pcm(int uid)
1127 {
1128         app_state_e state;
1129         if (0 > ttsd_data_get_client_state(uid, &state)) {
1130                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1131                 return TTSD_ERROR_INVALID_PARAMETER;
1132         }
1133
1134         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1135
1136         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1137                 if (0 != ttsd_player_clear(uid))
1138                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1139
1140                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1141         }
1142
1143         /* Reset all data */
1144         ttsd_data_clear_data(uid);
1145
1146         return TTSD_ERROR_NONE;
1147 }
1148
1149 int ttsd_server_add_pcm(int uid, int event, void* data, int data_size, int audio_type, int rate)
1150 {
1151         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1152
1153         int uttid = -1;
1154
1155         /* Synthesis is success */
1156         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1157                 if (TTSE_RESULT_EVENT_START == event) {
1158                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1159                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1160                                 uid, uttid, data, data_size, audio_type, rate);
1161                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1162                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1163                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
1164                                 uid, uttid, data, data_size, audio_type, rate);
1165                 } else {
1166                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1167                 }
1168
1169                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1170                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1171                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1172                         return TTSD_ERROR_INVALID_PARAMETER;
1173                 }
1174
1175                 /* add wav data */
1176                 sound_data_s* temp_sound_data = NULL;
1177                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
1178                 if (NULL == temp_sound_data) {
1179                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1180                         return TTSD_ERROR_OUT_OF_MEMORY;
1181                 }
1182
1183                 temp_sound_data->data = NULL;
1184                 temp_sound_data->rate = 0;
1185                 temp_sound_data->data_size = 0;
1186
1187                 if (0 < data_size) {
1188                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
1189                         if (NULL != temp_sound_data->data) {
1190                                 memcpy(temp_sound_data->data, data, data_size);
1191                                 temp_sound_data->data_size = data_size;
1192                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
1193                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
1194                         } else {
1195                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
1196                         }
1197                 } else {
1198                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
1199                 }
1200
1201                 temp_sound_data->utt_id = uttid;
1202                 temp_sound_data->event = event;
1203                 temp_sound_data->audio_type = audio_type;
1204                 temp_sound_data->rate = rate;
1205
1206                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
1207                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
1208
1209                         if (NULL != temp_sound_data->data) {
1210                                 free(temp_sound_data->data);
1211                                 temp_sound_data->data = NULL;
1212                         }
1213
1214                         free(temp_sound_data);
1215                         temp_sound_data = NULL;
1216                 }
1217
1218                 if (0 != ttsd_player_play(uid)) {
1219                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
1220
1221                         /* Change ready state */
1222                         ttsd_server_stop(uid);
1223
1224                         int tmp_pid;
1225                         tmp_pid = ttsd_data_get_pid(uid);
1226                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
1227                 }
1228         } else {
1229                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1230         }
1231
1232         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1233
1234         return TTSD_ERROR_NONE;
1235 }