Fix thread unsafety issue in ttsd_player_pause
[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 static bool g_is_paused;
47
48
49 /* Function definitions */
50 static int __synthesis(int uid, const char* credential);
51
52 static Eina_Bool __wait_synthesis(void *data)
53 {
54         /* get current play */
55         char* credential = (char*)data;
56         int uid = ttsd_data_get_current_playing();
57
58         if (uid > 0) {
59                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
60                         return EINA_TRUE;
61                 } else {
62                         g_wait_timer = NULL;
63                         if (TTSD_SYNTHESIS_CONTROL_DONE == ttsd_get_synth_control()) {
64                                 /* Start next synthesis */
65                                 __synthesis(uid, credential);
66                         }
67                 }
68         } else {
69                 g_wait_timer = NULL;
70         }
71
72         return EINA_FALSE;
73 }
74
75 static int __synthesis(int uid, const char* credential)
76 {
77         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  START");
78
79         speak_data_s* speak_data = NULL;
80         if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
81                 if (NULL == speak_data) {
82                         SLOG(LOG_WARN, tts_tag(), "[Server] speak data is null");
83                         return 0;
84                 }
85
86                 int pid = ttsd_data_get_pid(uid);
87                 char appid[1024] = {0, };
88                 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
89                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
90                 }
91
92                 if (NULL == speak_data->lang || NULL == speak_data->text) {
93                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
94                         ttsd_server_stop(uid);
95
96                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
97
98                         ttsd_data_clear_speak_data(uid, &speak_data);
99
100                         return 0;
101                 }
102
103                 g_utt.uid = uid;
104                 g_utt.uttid = speak_data->utt_id;
105
106                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
107                 SLOG(LOG_INFO, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
108                 SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
109                 SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
110                 SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
111                 SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
112
113                 int ret = 0;
114                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
115                 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
116                 if (0 != ret) {
117                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
118
119                         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
120
121                         ttsd_server_stop(uid);
122
123                         int pid = ttsd_data_get_pid(uid);
124                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
125                 } else {
126                         g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
127                 }
128
129                 ttsd_data_clear_speak_data(uid, &speak_data);
130         } else {
131                 ttsd_data_clear_speak_data(uid, &speak_data);
132         }
133
134         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
135
136         return 0;
137 }
138
139 /*
140 * TTS Server Callback Functions
141 */
142 int ttsd_send_error(ttse_error_e error, const char* msg)
143 {
144         int uid = g_utt.uid;
145         int uttid = g_utt.uttid;
146         int tmp_pid = ttsd_data_get_pid(uid);
147
148         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));
149
150         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
151
152         if (0 != ttsd_player_clear(uid))
153                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
154
155         if (0 != ttsd_data_clear_data(uid))
156                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
157
158         if (0 != ttsdc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
159                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_error_message()");
160
161         if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
162                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
163
164         if (0 != ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
165                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_send_set_state_message()");
166
167         return 0;
168 }
169
170 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)
171 {
172         SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
173
174         int uid = g_utt.uid;
175         int uttid = g_utt.uttid;
176
177         /* Synthesis is success */
178         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
179                 if (TTSE_RESULT_EVENT_START == event) {
180                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
181                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
182                                 uid, uttid, data, data_size, audio_type, rate);
183                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
184                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
185                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
186                                 uid, uttid, data, data_size, audio_type, rate);
187                 } else {
188                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
189                 }
190
191
192                 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
193                         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
194                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
195                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
196                         return TTSD_ERROR_OPERATION_FAILED;
197                 }
198
199                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
200                         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
201                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
202                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
203                         return TTSD_ERROR_INVALID_PARAMETER;
204                 }
205
206                 /* add wav data */
207                 sound_data_s* temp_sound_data = NULL;
208                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
209                 if (NULL == temp_sound_data) {
210                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
211                         return TTSD_ERROR_OUT_OF_MEMORY;
212                 }
213
214                 temp_sound_data->data = NULL;
215                 temp_sound_data->rate = 0;
216                 temp_sound_data->data_size = 0;
217
218                 if (0 < data_size) {
219                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
220                         if (NULL != temp_sound_data->data) {
221                                 memcpy(temp_sound_data->data, data, data_size);
222                                 temp_sound_data->data_size = data_size;
223                                 SLOG(LOG_INFO, tts_tag(), "[DEBUG][memcpy] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
224                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
225                         } else {
226                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
227                         }
228                 } else {
229                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
230                 }
231
232                 temp_sound_data->utt_id = uttid;
233                 temp_sound_data->event = event;
234                 temp_sound_data->audio_type = audio_type;
235                 temp_sound_data->rate = rate;
236
237                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
238                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
239                         if (NULL != temp_sound_data->data) {
240                                 free(temp_sound_data->data);
241                                 temp_sound_data->data = NULL;
242                         }
243
244                         free(temp_sound_data);
245                         temp_sound_data = NULL;
246
247                         return TTSD_ERROR_OPERATION_FAILED;
248                 }
249
250                 if (event == TTSE_RESULT_EVENT_FINISH) {
251                         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
252                 }
253
254                 /* If the app state is paused, do not result to play */
255                 if (true == g_is_paused) {
256                         SLOG(LOG_DEBUG, tts_tag(), "[Server DEBUG] tts_pause is called. Do not request to play");
257                         return TTSD_ERROR_NONE;
258                 }
259
260                 if (0 != ttsd_player_play(uid)) {
261                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
262
263                         /* Change ready state */
264                         ttsd_server_stop(uid);
265
266                         int tmp_pid;
267                         tmp_pid = ttsd_data_get_pid(uid);
268                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
269                 }
270         } else {
271                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
272                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
273         }
274
275
276         /*SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS RESULT CALLBACK END");
277         SLOG(LOG_DEBUG, tts_tag(), "  ");*/
278
279         return TTSD_ERROR_NONE;
280 }
281
282 bool __get_client_cb(int pid, int uid, app_tts_state_e state, void* user_data)
283 {
284         /* clear client data */
285         ttsd_data_clear_data(uid);
286         ttsd_data_set_client_state(uid, APP_STATE_READY);
287
288         /* send message */
289         if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
290                 /* remove client */
291                 ttsd_data_delete_client(uid);
292         }
293
294         return true;
295 }
296
297 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
298 {
299         switch (type) {
300         case TTS_CONFIG_TYPE_ENGINE:
301         {
302                 /* TODO - Determine the policy when engine process get engine changed cb */
303                 if (NULL == str_param) {
304                         SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
305                         return;
306                 }
307
308                 int ret = 0;
309                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
310                         SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
311                         ret = ttsd_engine_agent_unload_current_engine();
312                         if (0 != ret) {
313                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
314                         }
315
316                         ret = ttsd_engine_agent_load_current_engine();
317                         if (0 != ret) {
318                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
319                         }
320                         return;
321                 }
322
323                 /* stop all player */ 
324                 ttsd_player_all_stop();
325
326                 /* send interrupt message to  all clients */
327                 ttsd_data_foreach_clients(__get_client_cb, NULL);
328
329                 ttsd_engine_cancel_synthesis();
330
331                 break;
332         }
333
334         case TTS_CONFIG_TYPE_VOICE:
335         {
336                 if (NULL == str_param) {
337                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
338                         return;
339                 }
340
341                 char* out_lang = NULL;
342                 int out_type;
343                 int ret = -1;
344
345                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
346                         SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
347                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
348                         if (0 != ret)
349                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
350                 } else {
351                         /* Current language is not available */
352                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
353                 }
354                 if (NULL != out_lang) {
355                         free(out_lang);
356                         out_lang = NULL;
357                 }
358                 break;
359         }
360
361         case TTS_CONFIG_TYPE_SPEED:
362         {
363                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
364                         /* set default speed */
365                         int ret = 0;
366                         ret = ttsd_engine_agent_set_default_speed(int_param);
367                         if (0 != ret) {
368                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
369                         }
370                 }
371                 break;
372         }
373
374         case TTS_CONFIG_TYPE_PITCH:
375         {
376                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
377                         /* set default speed */
378                         int ret = 0;
379                         ret = ttsd_engine_agent_set_default_pitch(int_param);
380                         if (0 != ret) {
381                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
382                         }
383                 }
384                 break;
385         }
386         }
387
388         return;
389 }
390
391 bool __terminate_client(int pid, int uid, app_tts_state_e state, void* user_data)
392 {
393         SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
394         ttsd_server_finalize(uid);
395         return true;
396 }
397
398 Eina_Bool ttsd_terminate_daemon(void *data)
399 {
400         ttsd_data_foreach_clients(__terminate_client, NULL);
401         return EINA_FALSE;
402 }
403
404 void __screen_reader_changed_cb(bool value)
405 {
406         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
407                 SLOG(LOG_INFO, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
408                 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
409         } else {
410                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
411         }
412         return;
413 }
414
415 /*
416 * Server APIs
417 */
418 int ttsd_initialize(ttse_request_callback_s *callback)
419 {
420         SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
421
422         if (ttsd_config_initialize(__config_changed_cb)) {
423                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
424         }
425
426         /* player init */
427         if (ttsd_player_init()) {
428                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
429                 return TTSD_ERROR_OPERATION_FAILED;
430         }
431
432         /* Engine Agent initialize */
433         if (0 != ttsd_engine_agent_init()) {
434                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
435                 return TTSD_ERROR_OPERATION_FAILED;
436         }
437
438         /* set current engine */
439         //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
440         //      SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
441         //      g_is_engine = false;
442         //} else
443         //      g_is_engine = true;
444
445         if (0 != ttsd_engine_agent_load_current_engine(callback)) {
446                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
447                 return TTSD_ERROR_OPERATION_FAILED;
448         }
449
450         ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
451
452         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
453                 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
454         }
455
456         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
457         if (NULL == g_check_client_timer) {
458                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
459         }
460
461         return TTSD_ERROR_NONE;
462 }
463
464 int ttsd_finalize()
465 {
466         SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
467
468         GList *iter = NULL;
469         if (0 < g_list_length(g_proc_list)) {
470                 iter = g_list_first(g_proc_list);
471                 while (NULL != iter) {
472                         g_proc_list = g_list_remove_link(g_proc_list, iter);
473                         g_list_free(iter);
474                         iter = g_list_first(g_proc_list);
475                 }
476         }
477
478         ttsd_config_finalize();
479
480         ttsd_player_release();
481
482         ttsd_engine_agent_release();
483
484         if (NULL != g_check_client_timer) {
485                 ecore_timer_del(g_check_client_timer);
486                 g_check_client_timer = NULL;
487
488                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore timer handle");
489         }
490
491         return TTSD_ERROR_NONE;
492 }
493
494 int ttsd_terminate()
495 {
496         SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
497
498         ttsd_terminate_daemon(NULL);
499
500         ttsd_dbus_close_connection();
501         ttsd_network_finalize();
502         ttsd_finalize();
503
504         return TTSD_ERROR_NONE;
505 }
506
507 /*
508 * TTS Server Functions for Client
509 */
510
511 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
512 {
513         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
514
515         if (-1 != ttsd_data_is_client(uid)) {
516                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
517                 return TTSD_ERROR_NONE;
518         }
519
520         if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
521                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
522                 return TTSD_ERROR_OPERATION_FAILED;
523         }
524
525         if (true == *credential_needed) {
526                 char* appid = NULL;
527                 if (0 != app_manager_get_app_id(pid, &appid)) {
528                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
529                 }
530                 bool is_agreed = false;
531                 if (0 != ttsd_engine_check_app_agreed(appid, &is_agreed)) {
532                         SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
533                         if (!appid)
534                                 free(appid);
535                         return TTSD_ERROR_OPERATION_FAILED;
536                 }
537                 if (!appid)
538                         free(appid);
539
540                 if (false == is_agreed) {
541                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
542                         return TTSD_ERROR_PERMISSION_DENIED;
543                 }
544         }
545
546         if (0 != ttsd_data_new_client(pid, uid)) {
547                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
548                 return TTSD_ERROR_OPERATION_FAILED;
549         }
550
551         if (0 != ttsd_player_create_instance(uid)) {
552                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
553                 return TTSD_ERROR_OPERATION_FAILED;
554         }
555
556         return TTSD_ERROR_NONE;
557 }
558
559 static Eina_Bool __quit_ecore_loop(void *data)
560 {
561         ttsd_dbus_close_connection();
562         ttsd_network_finalize();
563         ttsd_finalize();
564         ecore_main_loop_quit();
565
566         return EINA_FALSE;
567 }
568
569
570 static void __read_proc()
571 {
572         DIR *dp = NULL;
573         struct dirent *dirp = NULL;
574         int tmp;
575
576         GList *iter = NULL;
577         if (0 < g_list_length(g_proc_list)) {
578                 iter = g_list_first(g_proc_list);
579                 while (NULL != iter) {
580                         g_proc_list = g_list_remove_link(g_proc_list, iter);
581                         g_list_free(iter);
582                         iter = g_list_first(g_proc_list);
583                 }
584         }
585
586         dp = opendir("/proc");
587         if (NULL == dp) {
588                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
589         } else {
590                 do {
591                         dirp = readdir(dp);
592
593                         if (NULL != dirp) {
594                                 tmp = atoi(dirp->d_name);
595                                 if (0 >= tmp)   continue;
596                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
597                         }
598                 } while (NULL != dirp);
599                 closedir(dp);
600         }
601         return;
602 }
603
604 bool __get_client_for_clean_up(int pid, int uid, app_tts_state_e state, void* user_data)
605 {
606         bool exist = false;
607         int i = 0;
608
609         GList *iter = NULL;
610         for (i = 0; i < g_list_length(g_proc_list); i++) {
611                 iter = g_list_nth(g_proc_list, i);
612                 if (NULL != iter) {
613                         if (pid == GPOINTER_TO_INT(iter->data)) {
614                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
615                                 exist = true;
616                                 break;
617                         }
618                 }
619         }
620
621         if (false == exist) {
622                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
623                 ttsd_server_finalize(uid);
624         }
625
626         return true;
627 #if 0
628         char appid[1024] = {0, };
629         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
630                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
631         }
632
633         if (0 < strlen(appid)) {
634                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
635         } else {
636                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
637
638                 int result = 1;
639                 result = ttsdc_send_hello(pid, uid);
640
641                 if (0 == result) {
642                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
643                         ttsd_server_finalize(uid);
644                 } else if (-1 == result) {
645                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
646                 }
647         }
648         return true;
649 #endif
650 }
651
652
653 Eina_Bool ttsd_cleanup_client(void *data)
654 {
655         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
656         __read_proc();
657
658         if (0 < ttsd_data_get_client_count()) {
659                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
660         } else {
661                 ecore_timer_add(0, __quit_ecore_loop, NULL);
662         }
663
664         SLOG(LOG_DEBUG, tts_tag(), "@@@");
665
666         return EINA_TRUE;
667 }
668
669 void __used_voice_cb(const char* lang, int type)
670 {
671         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
672         if (0 != ttsd_engine_unload_voice(lang, type)) {
673                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
674         }
675 }
676
677 int ttsd_server_finalize(int uid)
678 {
679         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
680
681         app_tts_state_e state;
682         if (0 > ttsd_data_get_client_state(uid, &state)) {
683                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
684         }
685
686         ttsd_server_stop(uid);
687         ttsd_player_stop(uid);
688
689         ttsd_player_destroy_instance(uid);
690
691         /* Need to unload voice when used voice is unregistered */
692         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
693                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
694                 return TTSD_ERROR_OPERATION_FAILED;
695         }
696
697         ttsd_data_delete_client(uid);
698
699         /* unload engine, if ref count of client is 0 */
700         if (0 == ttsd_data_get_client_count()) {
701                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
702                 ecore_timer_add(0, __quit_ecore_loop, NULL);
703         }
704
705         return TTSD_ERROR_NONE;
706 }
707
708 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
709 {
710         app_tts_state_e state;
711         if (0 > ttsd_data_get_client_state(uid, &state)) {
712                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
713                 return TTSD_ERROR_INVALID_PARAMETER;
714         }
715
716         /* check valid voice */
717         char* temp_lang = NULL;
718         int temp_type;
719         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
720                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
721                 if (NULL != temp_lang) {
722                         free(temp_lang);
723                         temp_lang = NULL;
724                 }
725                 return TTSD_ERROR_INVALID_VOICE;
726         }
727
728         if (NULL == temp_lang) {
729                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
730                 return TTSD_ERROR_INVALID_VOICE;
731         }
732
733         speak_data_s* speak_data = NULL;
734         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
735         if (NULL == speak_data) {
736                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
737                 if (NULL != temp_lang) {
738                         free(temp_lang);
739                         temp_lang = NULL;
740                 }
741                 return TTSD_ERROR_OPERATION_FAILED;
742         }
743
744         speak_data->lang = strdup(lang);
745         speak_data->vctype = voice_type;
746
747         speak_data->speed = speed;
748         speak_data->utt_id = utt_id;
749
750         speak_data->text = strdup(text);
751
752         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);
753
754         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
755         int ret = -1;
756         ret = ttsd_data_add_speak_data(uid, speak_data);
757         if (0 != ret) {
758                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
759                 if (NULL != temp_lang) {
760                         free(temp_lang);
761                         temp_lang = NULL;
762                 }
763                 if (NULL != speak_data) {
764                         if (NULL != speak_data->lang)   free(speak_data->lang);
765                         if (NULL != speak_data->text)   free(speak_data->text);
766
767                         speak_data->lang = NULL;
768                         speak_data->text = NULL;
769
770                         free(speak_data);
771                         speak_data = NULL;
772                 }
773
774                 return ret;
775         }
776
777         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
778                 /* Request load voice */
779                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
780                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
781                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
782                 }
783         }
784
785         if (NULL != temp_lang) {
786                 free(temp_lang);
787                 temp_lang = NULL;
788         }
789
790         if (APP_STATE_PLAYING == state) {
791                 /* check if engine use network */
792                 if (ttsd_engine_agent_need_network()) {
793                         if (false == ttsd_network_is_connected()) {
794                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
795                                 return TTSD_ERROR_OPERATION_FAILED;
796                         }
797                 }
798
799                 /* Check whether tts-engine is running or not */
800                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
801                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
802                 } else {
803                         __synthesis(uid, credential);
804                 }
805         }
806
807         return TTSD_ERROR_NONE;
808 }
809
810 Eina_Bool __send_interrupt_client(void *data)
811 {
812         intptr_t puid = (intptr_t)data;
813         int uid = (int)puid;
814
815         int pid = ttsd_data_get_pid(uid);
816
817         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
818                 /* send message to client about changing state */
819                 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
820         } else {
821                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
822         }
823
824         return EINA_FALSE;
825 }
826
827 int ttsd_server_play(int uid, const char* credential)
828 {
829         app_tts_state_e state;
830         if (0 > ttsd_data_get_client_state(uid, &state)) {
831                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
832                 return TTSD_ERROR_INVALID_PARAMETER;
833         }
834
835         if (APP_STATE_PLAYING == state) {
836                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
837                 return TTSD_ERROR_NONE;
838         }
839
840         /* check if engine use network */
841         if (ttsd_engine_agent_need_network()) {
842                 if (false == ttsd_network_is_connected()) {
843                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
844                         return TTSD_ERROR_OUT_OF_NETWORK;
845                 }
846         }
847
848         /* check the current playback focus */
849         if (TTSD_MODE_INTERRUPT != ttsd_get_mode()) {
850                 bool is_current_interrupt = false;
851                 if (0 != ttsd_player_check_current_playback_focus(&is_current_interrupt)) {
852                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to check the current playback focus");
853                 } else {
854                         if (true == is_current_interrupt) {
855                                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
856                                 ttsd_data_clear_data(uid);
857                                 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
858                         }
859                 }
860         }
861
862         int current_uid = ttsd_data_get_current_playing();
863         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%d)", current_uid);
864
865         if (uid != current_uid && -1 != current_uid) {
866                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
867                         /* Send interrupt message */
868                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
869
870                         /* pause player */
871                         if (0 != ttsd_server_stop(current_uid)) {
872                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
873                         }
874                         if (0 != ttsd_player_stop(current_uid)) {
875                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
876                         }
877
878                         intptr_t pcurrent_uid = (intptr_t)current_uid;
879                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
880                 } else {
881                         /* Default mode policy of interrupt is "Pause" */
882
883                         /* Send interrupt message */
884                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
885
886                         /* pause player */
887                         if (0 != ttsd_player_pause(current_uid)) {
888                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
889                         }
890
891                         /* change state */
892                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
893
894                         intptr_t pcurrent_uid = (intptr_t)current_uid;
895                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
896                 }
897         }
898
899         /* Change current play */
900         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
901                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
902                 return TTSD_ERROR_OPERATION_FAILED;
903         }
904
905         if (APP_STATE_PAUSED == state) {
906                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
907
908                 g_is_paused = false;
909
910                 /* Resume player */
911                 if (0 != ttsd_player_resume(uid)) {
912                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
913                 }
914         }
915
916         /* Check whether tts-engine is running or not */
917         if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control()) {
918                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
919         } else {
920                 __synthesis(uid, credential);
921         }
922
923         return TTSD_ERROR_NONE;
924 }
925
926 int ttsd_server_stop(int uid)
927 {
928         app_tts_state_e state;
929         if (0 > ttsd_data_get_client_state(uid, &state)) {
930                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
931                 return TTSD_ERROR_INVALID_PARAMETER;
932         }
933
934         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
935
936         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
937                 if (TTSD_SYNTHESIS_CONTROL_DOING == ttsd_get_synth_control() && uid == ttsd_data_get_current_playing()) {
938                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
939
940                         int ret = 0;
941                         ret = ttsd_engine_cancel_synthesis();
942                         if (0 != ret)
943                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
944                 }
945
946                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
947
948                 if (0 != ttsd_player_clear(uid))
949                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
950
951                 ttsd_data_set_client_state(uid, APP_STATE_READY);
952         }
953
954         /* Reset all data */
955         ttsd_data_clear_data(uid);
956         g_is_paused = false;
957
958         return TTSD_ERROR_NONE;
959 }
960
961 int ttsd_server_pause(int uid, int* utt_id)
962 {
963         app_tts_state_e state;
964         if (0 > ttsd_data_get_client_state(uid, &state)) {
965                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
966                 return TTSD_ERROR_INVALID_PARAMETER;
967         }
968
969         if (APP_STATE_PLAYING != state) {
970                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
971                 return TTSD_ERROR_INVALID_STATE;
972         }
973
974         g_is_paused = true;
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 }