Clean up code to enhance readability
[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_queue(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_queue : 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 static int __pause_client(unsigned int uid)
933 {
934         int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
935         if (TTSD_ERROR_NONE != ret) {
936                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
937                 return TTSD_ERROR_OPERATION_FAILED;
938         }
939
940         ret = ttsd_player_pause(uid);
941         if (TTSD_ERROR_NONE != ret) {
942                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
943                 return TTSD_ERROR_OPERATION_FAILED;
944         }
945
946         return TTSD_ERROR_NONE;
947 }
948
949 void __send_interrupt_client(unsigned int uid)
950 {
951         SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
952         int pid = ttsd_data_get_pid(uid);
953         if (pid <= 0) {
954                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
955                 return;
956         }
957
958         app_tts_state_e state = ttsd_data_get_client_state(uid);
959         if (APP_STATE_NONE == state) {
960                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
961                 return;
962         }
963
964         ttsdc_ipc_send_set_state_message(pid, uid, state);
965         SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
966         return;
967 }
968
969 int ttsd_server_play(unsigned int uid, const char* credential)
970 {
971         app_tts_state_e state = ttsd_data_get_client_state(uid);
972         if (APP_STATE_NONE == state) {
973                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
974                 return TTSD_ERROR_INVALID_PARAMETER;
975         }
976
977         if (APP_STATE_PLAYING == state) {
978                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
979                 return TTSD_ERROR_NONE;
980         }
981
982         /* check if engine use network */
983         if (ttsd_engine_agent_need_network()) {
984                 if (false == __is_connected_to_network()) {
985                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
986                         return TTSD_ERROR_OUT_OF_NETWORK;
987                 }
988         }
989
990         /* check the current playback focus */
991         if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
992                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
993                 ttsd_data_clear_data(uid);
994                 return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
995         }
996
997         unsigned int current_uid = ttsd_data_get_current_playing();
998         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
999
1000         if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1001                 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1002                         /* Send interrupt message */
1003                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1004
1005                         /* stop player */
1006                         if (0 != ttsd_server_stop(current_uid)) {
1007                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1008                         }
1009                 } else {
1010                         /* Default mode policy of interrupt is "Pause" */
1011
1012                         /* Send interrupt message */
1013                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1014
1015                         /* pause player */
1016                         if (0 != __pause_client(current_uid)) {
1017                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1018                         }
1019                 }
1020
1021                 __send_interrupt_client(current_uid);
1022         }
1023
1024         /* Change current play */
1025         if (0 != ttsd_player_wait_to_play(uid)) {
1026                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
1027         }
1028
1029         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1030                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1031                 return TTSD_ERROR_OPERATION_FAILED;
1032         }
1033
1034         int ret = TTSD_ERROR_NONE;
1035         if (APP_STATE_PAUSED == state) {
1036                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1037                 g_is_paused = false;
1038                 ret = ttsd_player_resume(uid);
1039         } else {
1040                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1041                 ret = ttsd_player_play(uid);
1042         }
1043
1044         if (TTSD_ERROR_NONE != ret) {
1045                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1046                 ttsd_data_set_client_state(uid, state);
1047                 return TTSD_ERROR_OPERATION_FAILED;
1048         }
1049
1050         ttsd_data_set_credential(uid, credential);
1051
1052         /* Check whether tts-engine is running or not */
1053         clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1054         ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1055         SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1056         if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1057                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1058         } else {
1059                 __synthesis(uid);
1060         }
1061
1062         return TTSD_ERROR_NONE;
1063 }
1064
1065 int ttsd_server_stop(unsigned int uid)
1066 {
1067         app_tts_state_e state = ttsd_data_get_client_state(uid);
1068         if (APP_STATE_NONE == state) {
1069                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1070                 return TTSD_ERROR_INVALID_PARAMETER;
1071         }
1072
1073         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1074
1075         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1076                 ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
1077                 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1078                 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1079                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1080
1081                         int ret = ttsd_engine_cancel_synthesis();
1082                         if (TTSD_ERROR_NONE != ret)
1083                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1084                 }
1085                 ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1086
1087                 /* stop player */
1088                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1089                 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1090                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1091                 }
1092         }
1093
1094         /* Reset all data */
1095         ttsd_data_clear_data(uid);
1096         g_is_paused = false;
1097
1098         return TTSD_ERROR_NONE;
1099 }
1100
1101 int ttsd_server_pause(unsigned int uid, int* utt_id)
1102 {
1103         app_tts_state_e state = ttsd_data_get_client_state(uid);
1104         if (APP_STATE_NONE == state) {
1105                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1106                 return TTSD_ERROR_INVALID_PARAMETER;
1107         }
1108
1109         if (APP_STATE_PLAYING != state) {
1110                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1111                 return TTSD_ERROR_INVALID_STATE;
1112         }
1113
1114         g_is_paused = true;
1115
1116         *utt_id = g_utt.uttid;
1117         SLOG(LOG_INFO, tts_tag(), "[Server] server pause, uid(%u), g_uid(%u), utt_id(%d)", uid, g_utt.uid, *utt_id);
1118
1119         int ret = __pause_client(uid);
1120         if (TTSD_ERROR_NONE != ret) {
1121                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to pause player : ret(%d)", ret);
1122                 return TTSD_ERROR_OPERATION_FAILED;
1123         }
1124
1125         return TTSD_ERROR_NONE;
1126 }
1127
1128 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1129 {
1130         if (0 > ttsd_data_is_client(uid)) {
1131                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1132                 return TTSD_ERROR_INVALID_PARAMETER;
1133         }
1134
1135         /* get voice list*/
1136         int ret = ttsd_engine_get_voice_list(voice_list);
1137         if (0 != ret) {
1138                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1139                 return ret;
1140         }
1141
1142         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1143
1144         return TTSD_ERROR_NONE;
1145 }
1146
1147 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1148 {
1149         if (0 > ttsd_data_is_client(uid)) {
1150                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1151                 return TTSD_ERROR_INVALID_PARAMETER;
1152         }
1153
1154         /* get current voice */
1155         int ret = ttsd_engine_get_default_voice(language, voice_type);
1156         if (0 != ret) {
1157                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1158                 return ret;
1159         }
1160
1161         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1162
1163         return TTSD_ERROR_NONE;
1164 }
1165
1166 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1167 {
1168         app_tts_state_e state = ttsd_data_get_client_state(uid);
1169         if (APP_STATE_NONE == state) {
1170                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1171                 return TTSD_ERROR_INVALID_PARAMETER;
1172         }
1173
1174         if (APP_STATE_READY != state) {
1175                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1176                 return TTSD_ERROR_INVALID_STATE;
1177         }
1178
1179         int ret = ttsd_engine_set_private_data(key, data);
1180         if (0 != ret) {
1181                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1182         } else {
1183                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1184         }
1185
1186         return ret;
1187 }
1188
1189 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1190 {
1191         app_tts_state_e state = ttsd_data_get_client_state(uid);
1192         if (APP_STATE_NONE == state) {
1193                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1194                 return TTSD_ERROR_INVALID_PARAMETER;
1195         }
1196
1197         if (APP_STATE_READY != state) {
1198                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1199                 return TTSD_ERROR_INVALID_STATE;
1200         }
1201
1202         int ret = ttsd_engine_get_private_data(key, data);
1203         if (0 != ret) {
1204                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1205         } else {
1206                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1207         }
1208
1209         return ret;
1210 }
1211
1212 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1213 {
1214         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1215
1216         int ret = 0;
1217         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1218         if (0 != ret) {
1219                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1220         }
1221
1222         return ret;
1223 }
1224
1225 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1226 {
1227         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1228
1229         int ret = 0;
1230         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1231         if (0 != ret) {
1232                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1233         }
1234
1235         return ret;
1236 }
1237
1238 int ttsd_server_play_pcm(unsigned int uid)
1239 {
1240         app_tts_state_e state = ttsd_data_get_client_state(uid);
1241         if (APP_STATE_NONE == state) {
1242                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1243                 return TTSD_ERROR_INVALID_PARAMETER;
1244         }
1245
1246         if (APP_STATE_PLAYING == state) {
1247                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1248                 return TTSD_ERROR_NONE;
1249         }
1250
1251         unsigned int current_uid = ttsd_data_get_current_playing();
1252         SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
1253
1254         if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
1255                 if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
1256                         /* Send interrupt message */
1257                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
1258
1259                         /* stop player */
1260                         if (0 != ttsd_server_stop(current_uid)) {
1261                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
1262                         }
1263                 } else {
1264                         /* Default mode policy of interrupt is "Pause" */
1265
1266                         /* Send interrupt message */
1267                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
1268
1269                         /* pause player */
1270                         if (0 != __pause_client(current_uid)) {
1271                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
1272                         }
1273                 }
1274
1275                 __send_interrupt_client(current_uid);
1276         }
1277
1278         /* Change current play */
1279         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1280                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1281                 return TTSD_ERROR_OPERATION_FAILED;
1282         }
1283
1284         int ret = TTSD_ERROR_NONE;
1285         if (APP_STATE_PAUSED == state) {
1286                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
1287                 g_is_paused = false;
1288                 ret = ttsd_player_resume(uid);
1289         } else {
1290                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1291                 ret = ttsd_player_play(uid);
1292         }
1293
1294         if (TTSD_ERROR_NONE != ret) {
1295                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1296                 ttsd_data_set_client_state(uid, state);
1297                 return TTSD_ERROR_OPERATION_FAILED;
1298         }
1299
1300         return TTSD_ERROR_NONE;
1301 }
1302
1303 int ttsd_server_stop_pcm(unsigned int uid)
1304 {
1305         app_tts_state_e state = ttsd_data_get_client_state(uid);
1306         if (APP_STATE_NONE == state) {
1307                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1308                 return TTSD_ERROR_INVALID_PARAMETER;
1309         }
1310
1311         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, state(%d)", state);
1312
1313         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state || APP_STATE_READY == state) {
1314                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1315         }
1316
1317         /* Reset all data */
1318         ttsd_data_clear_data(uid);
1319
1320         ttsd_player_stop(uid);
1321
1322         return TTSD_ERROR_NONE;
1323 }
1324
1325 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1326 {
1327         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1328
1329         int uttid = -1;
1330
1331         /* Synthesis is success */
1332         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1333                 if (TTSE_RESULT_EVENT_START == event) {
1334                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1335                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1336                                 uid, uttid, data, data_size, audio_type, rate);
1337                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1338                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1339                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1340                                 uid, uttid, data, data_size, audio_type, rate);
1341                 } else {
1342                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1343                 }
1344
1345                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1346                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1347                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1348                         return TTSD_ERROR_INVALID_PARAMETER;
1349                 }
1350
1351                 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1352                 if (NULL == sound_data) {
1353                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1354                         return TTSD_ERROR_OUT_OF_MEMORY;
1355                 }
1356
1357                 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1358                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1359                         ttsd_data_destroy_sound_data(sound_data);
1360                 }
1361         } else {
1362                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1363         }
1364
1365         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1366
1367         return TTSD_ERROR_NONE;
1368 }