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