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