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