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