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