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