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