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