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