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