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