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