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