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