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