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