Change to TTS engine process
[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 <aul.h>
15 #include <Ecore.h>
16
17 #include "ttsd_config.h"
18 #include "ttsd_data.h"
19 #include "ttsd_dbus.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_engine_agent.h"
22 #include "ttsd_main.h"
23 #include "ttsd_network.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
26
27
28 typedef enum {
29         TTSD_SYNTHESIS_CONTROL_DOING    = 0,
30         TTSD_SYNTHESIS_CONTROL_DONE     = 1,
31         TTSD_SYNTHESIS_CONTROL_EXPIRED  = 2
32 } ttsd_synthesis_control_e;
33
34 typedef struct {
35         int uid;
36         int uttid;
37 } utterance_t;
38
39 /* If current engine exist */
40 //static bool   g_is_engine;
41
42 /* If engine is running */
43 static ttsd_synthesis_control_e g_synth_control;
44
45 static Ecore_Timer* g_wait_timer = NULL;
46
47 static utterance_t g_utt;
48
49 static GList *g_proc_list = NULL;
50
51 /* Function definitions */
52 static int __synthesis(int uid, const char* credential);
53
54 static int __server_set_synth_control(ttsd_synthesis_control_e control)
55 {
56         g_synth_control = control;
57         return 0;
58 }
59
60 static ttsd_synthesis_control_e __server_get_synth_control()
61 {
62         return g_synth_control;
63 }
64
65 static Eina_Bool __wait_synthesis(void *data)
66 {
67         /* get current play */
68         char* credential = (char*)data;
69         int uid = ttsd_data_get_current_playing();
70
71         if (uid > 0) {
72                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
73                         usleep(100000);
74                         return EINA_TRUE;
75                 } else {
76                         g_wait_timer = NULL;
77                         if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
78                                 /* Start next synthesis */
79                                 __synthesis(uid, credential);
80                         }
81                 }
82         } else {
83                 g_wait_timer = NULL;
84         }
85
86         return EINA_FALSE;
87 }
88
89 static int __synthesis(int uid, const char* credential)
90 {
91         SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS  START");
92
93         speak_data_s* speak_data = NULL;
94         if (0 == ttsd_data_get_speak_data(uid, &speak_data)) {
95                 if (NULL == speak_data) {
96                         return 0;
97                 }
98
99                 int pid = ttsd_data_get_pid(uid);
100                 char appid[128] = {0, };
101                 if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
102                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
103                 }
104
105                 if (NULL == speak_data->lang || NULL == speak_data->text) {
106                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
107                         ttsd_server_stop(uid);
108
109                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
110
111                         if (NULL != speak_data) {
112                                 if (NULL != speak_data->lang)   free(speak_data->lang);
113                                 if (NULL != speak_data->text)   free(speak_data->text);
114
115                                 speak_data->lang = NULL;
116                                 speak_data->text = NULL;
117
118                                 free(speak_data);
119                                 speak_data = NULL;
120                         }
121
122                         return 0;
123                 }
124
125                 g_utt.uid = uid;
126                 g_utt.uttid = speak_data->utt_id;
127
128                 SLOG(LOG_DEBUG, tts_tag(), "-----------------------------------------------------------");
129                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
130                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
131                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Text : %s", speak_data->text);
132                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "Credential : %s", credential);
133                 SLOG(LOG_DEBUG, tts_tag(), "-----------------------------------------------------------");
134
135                 int ret = 0;
136                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
137                 ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
138                 if (0 != ret) {
139                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
140
141                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
142
143                         ttsd_server_stop(uid);
144
145                         int pid = ttsd_data_get_pid(uid);
146                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
147                 } else {
148                         g_wait_timer = ecore_timer_add(0, __wait_synthesis, (void*)credential);
149                 }
150
151                 if (NULL != speak_data) {
152                         if (NULL != speak_data->lang)   free(speak_data->lang);
153                         if (NULL != speak_data->text)   free(speak_data->text);
154
155                         speak_data->lang = NULL;
156                         speak_data->text = NULL;
157
158                         free(speak_data);
159                         speak_data = NULL;
160                 }
161         }
162
163         SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS  END");
164         SLOG(LOG_DEBUG, tts_tag(), "  ");
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         return 0;
175 }
176
177 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)
178
179 {
180         SLOG(LOG_DEBUG, tts_tag(), "===== SEND SYNTHESIS RESULT START");
181
182         int uid = g_utt.uid;
183         int uttid = g_utt.uttid;
184
185         /* Synthesis is success */
186         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
187                 if (TTSE_RESULT_EVENT_START == event) {
188                         SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
189                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
190                                 uid, uttid, data, data_size, audio_type, rate);
191                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
192                         SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
193                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
194                                 uid, uttid, data, data_size, audio_type, rate);
195                 } else {
196                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
197                 }
198
199
200                 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
201                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
202                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
203                         SLOG(LOG_DEBUG, tts_tag(), "=====");
204                         SLOG(LOG_DEBUG, tts_tag(), "  ");
205                         return 0;
206                 }
207
208                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
209                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
210                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
211                         SLOG(LOG_DEBUG, tts_tag(), "=====");
212                         SLOG(LOG_DEBUG, tts_tag(), "  ");
213                         return 0;
214                 }
215
216                 /* add wav data */
217                 sound_data_s* temp_sound_data = NULL;
218                 temp_sound_data = (sound_data_s*)calloc(1, sizeof(sound_data_s));
219                 if (NULL == temp_sound_data) {
220                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
221                         return 0;
222                 }
223                 
224                 temp_sound_data->data = NULL;
225                 temp_sound_data->rate = 0;
226                 temp_sound_data->data_size = 0;
227
228                 if (0 < data_size) {
229                         temp_sound_data->data = (char*)calloc(data_size + 5, sizeof(char));
230                         if (NULL != temp_sound_data->data) {
231                                 memcpy(temp_sound_data->data, data, data_size);
232                                 temp_sound_data->data_size = data_size;
233                                 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][free] uid(%d), event(%d) sound_data(%p) data(%p) size(%d)", 
234                                         uid, event, temp_sound_data, temp_sound_data->data, temp_sound_data->data_size);
235                         } else {
236                                 SLOG(LOG_ERROR, tts_tag(), "Fail to allocate memory");
237                         }
238                 } else {
239                         SLOG(LOG_ERROR, tts_tag(), "Sound data is NULL");
240                 }
241
242                 temp_sound_data->utt_id = uttid;
243                 temp_sound_data->event = event;
244                 temp_sound_data->audio_type = audio_type;
245                 temp_sound_data->rate = rate;
246
247                 if (0 != ttsd_data_add_sound_data(uid, temp_sound_data)) {
248                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
249                 }
250
251                 if (event == TTSE_RESULT_EVENT_FINISH) {
252                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
253                 }
254
255                 if (0 != ttsd_player_play(uid)) {
256                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
257
258                         /* Change ready state */
259                         ttsd_server_stop(uid);
260
261                         int tmp_pid;
262                         tmp_pid = ttsd_data_get_pid(uid);
263                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
264                 }
265         } else {
266                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
267                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
268         }
269
270
271         /*SLOG(LOG_DEBUG, tts_tag(), "===== SYNTHESIS RESULT CALLBACK END");
272         SLOG(LOG_DEBUG, tts_tag(), "  ");*/
273
274         return 0;
275 }
276
277 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
278 {
279         /* clear client data */
280         ttsd_data_clear_data(uid);
281         ttsd_data_set_client_state(uid, APP_STATE_READY);
282
283         /* send message */
284         if (0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
285                 /* remove client */
286                 ttsd_data_delete_client(uid);
287         }
288
289         return true;
290 }
291
292 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
293 {
294         switch (type) {
295         case TTS_CONFIG_TYPE_ENGINE:
296         {
297                 /* TODO - Determine the policy when engine process get engine changed cb */
298                 if (NULL == str_param) {
299                         SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
300                         return;
301                 }
302
303                 int ret = 0;
304                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
305                         SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
306                         ret = ttsd_engine_agent_unload_current_engine();
307                         if (0 != ret) {
308                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
309                         }
310
311                         ret = ttsd_engine_agent_load_current_engine();
312                         if (0 != ret) {
313                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
314                         }
315                         return;
316                 }
317
318                 /* stop all player */ 
319                 ttsd_player_all_stop();
320
321                 /* send interrupt message to  all clients */
322                 ttsd_data_foreach_clients(__get_client_cb, NULL);
323
324                 ttsd_engine_cancel_synthesis();
325
326                 break;
327         }
328
329         case TTS_CONFIG_TYPE_VOICE:
330         {
331                 if (NULL == str_param) {
332                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
333                         return;
334                 }
335
336                 char* out_lang = NULL;
337                 int out_type;
338                 int ret = -1;
339
340                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
341                         SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
342                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
343                         if (0 != ret)
344                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
345                 } else {
346                         /* Current language is not available */
347                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
348                 }
349                 if (NULL != out_lang)   free(out_lang);
350                 break;
351         }
352
353         case TTS_CONFIG_TYPE_SPEED:
354         {
355                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
356                         /* set default speed */
357                         int ret = 0;
358                         ret = ttsd_engine_agent_set_default_speed(int_param);
359                         if (0 != ret) {
360                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
361                         }       
362                 }
363                 break;
364         }
365
366         case TTS_CONFIG_TYPE_PITCH:
367         {
368                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
369                         /* set default speed */
370                         int ret = 0;
371                         ret = ttsd_engine_agent_set_default_pitch(int_param);
372                         if (0 != ret) {
373                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
374                         }       
375                 }
376                 break;
377         }
378
379         default:
380                 break;
381         }
382         
383         return;
384 }
385
386 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
387 {
388         SLOG(LOG_DEBUG, tts_tag(), "=== Start to terminate client [%d] ===", uid);
389         ttsd_server_finalize(uid);
390         return true;
391 }
392
393 Eina_Bool ttsd_terminate_daemon(void *data) 
394 {
395         ttsd_data_foreach_clients(__terminate_client, NULL);
396         return EINA_FALSE;
397 }
398
399 void __screen_reader_changed_cb(bool value)
400 {
401         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
402                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
403                 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
404         } else {
405                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
406         }
407         return;
408 }
409
410 /*
411 * Server APIs
412 */
413 static bool __send_reset_signal(int pid, int uid, app_state_e state, void* user_data)
414 {
415         ttsdc_send_error_message(pid, uid, -1, TTSD_ERROR_SERVICE_RESET, "TTS service reset");
416         return true;
417 }
418
419 static void __sig_handler(int signo)
420 {
421         /* restore signal handler */
422         signal(signo, SIG_DFL);
423
424         /* Send error signal via foreach clients */
425         ttsd_data_foreach_clients(__send_reset_signal, NULL);
426
427         /* invoke signal again */
428         raise(signo);
429 }
430
431 static void __register_sig_handler()
432 {
433         signal(SIGSEGV, __sig_handler);
434         signal(SIGABRT, __sig_handler);
435         signal(SIGTERM, __sig_handler);
436         signal(SIGINT, __sig_handler);
437         signal(SIGQUIT, __sig_handler);
438 }
439
440 int ttsd_initialize(ttse_request_callback_s *callback)
441 {
442         /* Register signal handler */
443         __register_sig_handler();
444         
445         if (ttsd_config_initialize(__config_changed_cb)) {
446                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
447         }
448
449         /* player init */
450         if (ttsd_player_init()) {
451                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
452                 return TTSD_ERROR_OPERATION_FAILED;
453         }
454
455         /* Engine Agent initialize */
456         if (0 != ttsd_engine_agent_init()) {
457                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
458                 return TTSD_ERROR_OPERATION_FAILED;
459         }
460
461         /* set current engine */
462         //if (0 != ttsd_engine_agent_initialize_current_engine(callback)) {
463         //      SLOG(LOG_WARN, tts_tag(), "[Server WARNING] No Engine !!!" );
464         //      g_is_engine = false;
465         //} else
466         //      g_is_engine = true;
467
468         if (0 != ttsd_engine_agent_load_current_engine(callback)) {
469                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
470                 return TTSD_ERROR_OPERATION_FAILED;
471         }
472
473         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
474
475         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
476                 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
477         }
478
479         return TTSD_ERROR_NONE;
480 }
481
482 int ttsd_finalize()
483 {
484         GList *iter = NULL;
485         if (0 < g_list_length(g_proc_list)) {
486                 iter = g_list_first(g_proc_list);
487                 while (NULL != iter) {
488                         g_proc_list = g_list_remove_link(g_proc_list, iter);
489                         g_list_free(iter);
490                         iter = g_list_first(g_proc_list);
491                 }
492         }
493         
494         ttsd_config_finalize();
495
496         ttsd_player_release();
497
498         ttsd_engine_agent_release();
499
500         return TTSD_ERROR_NONE;
501 }
502
503 /*
504 * TTS Server Functions for Client
505 */
506
507 int ttsd_server_initialize(int pid, int uid, bool* credential_needed)
508 {
509         if (-1 != ttsd_data_is_client(uid)) {
510                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
511                 return TTSD_ERROR_NONE;
512         }
513
514         if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
515                 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to get credential necessity");
516                 return TTSD_ERROR_OPERATION_FAILED;
517         }
518         if (0 != ttsd_data_new_client(pid, uid)) {
519                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
520                 return TTSD_ERROR_OPERATION_FAILED;
521         }
522
523         if (0 != ttsd_player_create_instance(uid)) {
524                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to create player");
525                 return TTSD_ERROR_OPERATION_FAILED;
526         }
527
528         return TTSD_ERROR_NONE;
529 }
530
531 static Eina_Bool __quit_ecore_loop(void *data)
532 {
533         ttsd_dbus_close_connection();
534
535         ttsd_network_finalize();
536
537         ttsd_finalize();
538
539         ecore_main_loop_quit();
540         return EINA_FALSE;
541 }
542
543
544 static void __read_proc()
545 {
546         DIR *dp = NULL;
547         struct dirent entry;
548         struct dirent *dirp = NULL;
549         int ret = -1;
550         int tmp;
551
552         GList *iter = NULL;
553         if (0 < g_list_length(g_proc_list)) {
554                 iter = g_list_first(g_proc_list);
555                 while (NULL != iter) {
556                         g_proc_list = g_list_remove_link(g_proc_list, iter);
557                         g_list_free(iter);
558                         iter = g_list_first(g_proc_list);
559                 }
560         }
561
562         dp = opendir("/proc");
563         if (NULL == dp) {
564                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
565         } else {
566                 do {
567                         ret = readdir_r(dp, &entry, &dirp);
568                         if (0 != ret) {
569                                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to readdir");
570                                 break;
571                         }
572
573                         if (NULL != dirp) {
574                                 tmp = atoi(dirp->d_name);
575                                 if (0 >= tmp)   continue;
576                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
577                         }
578                 } while (NULL != dirp);
579                 closedir(dp);
580         }
581         return;
582 }
583
584 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
585 {
586         bool exist = false;
587         int i = 0;
588         
589         GList *iter = NULL;
590         for (i = 0; i < g_list_length(g_proc_list); i++) {
591                 iter = g_list_nth(g_proc_list, i);
592                 if (NULL != iter) {
593                         if (pid == GPOINTER_TO_INT(iter->data)) {
594                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%d) is running", uid);
595                                 exist = true;
596                                 break;
597                         }
598                 }
599         }
600         
601         if (false == exist) {
602                 SLOG(LOG_ERROR, tts_tag(), "uid (%d) should be removed", uid);
603                 ttsd_server_finalize(uid);
604         }
605
606         return true;
607 #if 0
608         char appid[128] = {0, };
609         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
610                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
611         }
612
613         if (0 < strlen(appid)) {
614                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is running app - %s", pid, appid);
615         } else {
616                 SLOG(LOG_DEBUG, tts_tag(), "[%d] is daemon or no_running app", pid);
617
618                 int result = 1;
619                 result = ttsdc_send_hello(pid, uid);
620
621                 if (0 == result) {
622                         SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) should be removed.", uid);
623                         ttsd_server_finalize(uid);
624                 } else if (-1 == result) {
625                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Hello result has error");
626                 }
627         }
628         return true;
629 #endif
630 }
631
632
633 Eina_Bool ttsd_cleanup_client(void *data)
634 {
635         SLOG(LOG_DEBUG, tts_tag(), "===== CLEAN UP CLIENT START");
636         __read_proc();
637
638         if (0 < ttsd_data_get_client_count()) {
639         ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
640                 } else {
641                 ecore_timer_add(0, __quit_ecore_loop, NULL);
642         }
643
644         SLOG(LOG_DEBUG, tts_tag(), "=====");
645         SLOG(LOG_DEBUG, tts_tag(), "  ");
646
647         return EINA_TRUE;
648 }
649
650 void __used_voice_cb(const char* lang, int type)
651 {
652         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
653         if (0 != ttsd_engine_unload_voice(lang, type)) {
654                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
655         }
656 }
657
658 int ttsd_server_finalize(int uid)
659 {
660         app_state_e state;
661         if (0 > ttsd_data_get_client_state(uid, &state)) {
662                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
663         }
664
665         ttsd_server_stop(uid);
666         ttsd_player_stop(uid);
667         
668         ttsd_player_destroy_instance(uid);
669
670         /* Need to unload voice when used voice is unregistered */
671         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
672                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
673                 return TTSD_ERROR_OPERATION_FAILED;
674         }
675
676         ttsd_data_delete_client(uid);
677
678         /* unload engine, if ref count of client is 0 */
679         if (0 == ttsd_data_get_client_count()) {
680                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
681                 ecore_timer_add(0, __quit_ecore_loop, NULL);
682         }
683
684         return TTSD_ERROR_NONE;
685 }
686
687 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
688 {
689         app_state_e state;
690         if (0 > ttsd_data_get_client_state(uid, &state)) {
691                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
692                 return TTSD_ERROR_INVALID_PARAMETER;
693         }
694
695         /* check valid voice */
696         char* temp_lang = NULL;
697         int temp_type;
698         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
699                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
700                 if (NULL != temp_lang)  free(temp_lang);
701                 return TTSD_ERROR_INVALID_VOICE;
702         }
703
704         if (NULL == temp_lang) {
705                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
706                 return TTSD_ERROR_INVALID_VOICE;
707         }
708         
709         speak_data_s* speak_data = NULL;
710         speak_data = (speak_data_s*)calloc(1, sizeof(speak_data_s));
711         if (NULL == speak_data) {
712                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
713                 if (NULL != temp_lang)  free(temp_lang);
714                 return TTSD_ERROR_OPERATION_FAILED;
715         }
716
717         speak_data->lang = strdup(lang);
718         speak_data->vctype = voice_type;
719
720         speak_data->speed = speed;
721         speak_data->utt_id = utt_id;
722
723         speak_data->text = strdup(text);
724
725         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
726         if (0 != ttsd_data_add_speak_data(uid, speak_data)) {
727                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
728                 if (NULL != temp_lang)  free(temp_lang);
729                 if (NULL != speak_data) {
730                         if (NULL != speak_data->lang)   free(speak_data->lang);
731                         if (NULL != speak_data->text)   free(speak_data->text);
732
733                         speak_data->lang = NULL;
734                         speak_data->text = NULL;
735
736                         free(speak_data);
737                         speak_data = NULL;
738                 }
739
740                 return TTSD_ERROR_OPERATION_FAILED;
741         }
742
743         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
744                 /* Request load voice */
745                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
746                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
747                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
748                 }
749         }
750
751         if (NULL != temp_lang)  free(temp_lang);
752
753         if (APP_STATE_PLAYING == state) {
754                 /* check if engine use network */
755                 if (ttsd_engine_agent_need_network()) {
756                         if (false == ttsd_network_is_connected()) {
757                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
758                                 return TTSD_ERROR_OPERATION_FAILED;
759                         }
760                 }
761
762                 /* Check whether tts-engine is running or not */
763                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
764                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
765                 } else {
766                         __synthesis(uid, credential);
767                 }
768         }
769
770         return TTSD_ERROR_NONE;
771 }
772
773 Eina_Bool __send_interrupt_client(void *data)
774 {
775         intptr_t puid = (intptr_t)data;
776         int uid = (int)puid;
777
778         int pid = ttsd_data_get_pid(uid);
779
780         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
781                 /* send message to client about changing state */
782                 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
783         } else {
784                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
785         }
786
787         return EINA_FALSE;
788 }
789
790 int ttsd_server_play(int uid, const char* credential)
791 {
792         app_state_e state;
793         if (0 > ttsd_data_get_client_state(uid, &state)) {
794                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
795                 return TTSD_ERROR_INVALID_PARAMETER;
796         }
797
798         if (APP_STATE_PLAYING == state) {
799                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
800                 return TTSD_ERROR_NONE;
801         }
802
803         /* check if engine use network */
804         if (ttsd_engine_agent_need_network()) {
805                 if (false == ttsd_network_is_connected()) {
806                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
807                         return TTSD_ERROR_OUT_OF_NETWORK;
808                 }
809         }
810
811         int current_uid = ttsd_data_get_current_playing();
812         SLOG(LOG_ERROR, tts_tag(), "[Server] playing uid (%d)", current_uid);
813
814         if (uid != current_uid && -1 != current_uid) {
815                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
816                         /* Send interrupt message */
817                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
818
819                         /* pause player */
820                         if (0 != ttsd_server_stop(current_uid)) {
821                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
822                         }
823                         if (0 != ttsd_player_stop(current_uid)) {
824                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to player stop : uid (%d)", current_uid);
825                         }
826
827                         intptr_t pcurrent_uid = (intptr_t)current_uid;
828                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
829                 } else {
830                         /* Default mode policy of interrupt is "Pause" */
831
832                         /* Send interrupt message */
833                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
834
835                         /* pause player */
836                         if (0 != ttsd_player_pause(current_uid)) {
837                                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
838                         }
839
840                         /* change state */
841                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
842
843                         intptr_t pcurrent_uid = (intptr_t)current_uid;
844                         ecore_timer_add(0, __send_interrupt_client, (void*)pcurrent_uid);
845                 }
846         }
847
848         /* Change current play */
849         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
850                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
851                 return TTSD_ERROR_OPERATION_FAILED;
852         }
853
854         if (APP_STATE_PAUSED == state) {
855                 SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
856
857                 /* Resume player */
858                 if (0 != ttsd_player_resume(uid)) {
859                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
860                 }
861         }
862
863         /* Check whether tts-engine is running or not */
864         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
865                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
866         } else {
867                 __synthesis(uid, credential);
868         }
869
870         return TTSD_ERROR_NONE;
871 }
872
873
874 int ttsd_server_stop(int uid)
875 {
876         app_state_e state;
877         if (0 > ttsd_data_get_client_state(uid, &state)) {
878                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
879                 return TTSD_ERROR_INVALID_PARAMETER;
880         }
881
882         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
883                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
884                         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
885
886                         int ret = 0;
887                         ret = ttsd_engine_cancel_synthesis();
888                         if (0 != ret)
889                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
890                 }
891
892                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
893
894                 if (0 != ttsd_player_clear(uid))
895                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
896
897                 ttsd_data_set_client_state(uid, APP_STATE_READY);
898         }
899
900         /* Reset all data */
901         ttsd_data_clear_data(uid);
902
903         return TTSD_ERROR_NONE;
904 }
905
906 int ttsd_server_pause(int uid, int* utt_id)
907 {
908         app_state_e state;
909         if (0 > ttsd_data_get_client_state(uid, &state)) {
910                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
911                 return TTSD_ERROR_INVALID_PARAMETER;
912         }
913
914         if (APP_STATE_PLAYING != state) {
915                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
916                 return TTSD_ERROR_INVALID_STATE;
917         }
918
919         int ret = 0;
920         ret = ttsd_player_pause(uid);
921         if (0 != ret) {
922                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
923                 return TTSD_ERROR_OPERATION_FAILED;
924         }
925
926         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
927
928         return TTSD_ERROR_NONE;
929 }
930
931 int ttsd_server_get_support_voices(int uid, GList** voice_list)
932 {
933         app_state_e state;
934         if (0 > ttsd_data_get_client_state(uid, &state)) {
935                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
936                 return TTSD_ERROR_INVALID_PARAMETER;
937         }
938
939         /* get voice list*/
940         int ret = ttsd_engine_get_voice_list(voice_list); 
941         if (0 != ret) {
942                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
943                 return ret;
944         }
945
946         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
947
948         return TTSD_ERROR_NONE;
949 }
950
951 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
952 {
953         app_state_e state;
954         if (0 > ttsd_data_get_client_state(uid, &state)) {
955                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
956                 return TTSD_ERROR_INVALID_PARAMETER;
957         }
958
959         /* get current voice */
960         int ret = ttsd_engine_get_default_voice(language, voice_type);
961         if (0 != ret) {
962                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
963                 return ret;
964         }
965
966         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
967
968         return TTSD_ERROR_NONE;
969 }
970
971 int ttsd_server_set_private_data(int uid, const char* key, const char* data)
972 {
973         app_state_e state;
974         if (0 > ttsd_data_get_client_state(uid, &state)) {
975                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
976                 return TTSD_ERROR_INVALID_PARAMETER;
977         }
978
979         if (APP_STATE_READY != state) {
980                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
981                 return TTSD_ERROR_INVALID_STATE;
982         }
983
984         int ret = ttsd_engine_set_private_data(key, data);
985         if (0 != ret) {
986                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
987         } else {
988                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data");
989         }
990
991         return ret;
992 }
993
994 int ttsd_server_get_private_data(int uid, const char* key, char** data)
995 {
996         app_state_e state;
997         if (0 > ttsd_data_get_client_state(uid, &state)) {
998                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%d) is NOT valid", uid);
999                 return TTSD_ERROR_INVALID_PARAMETER;
1000         }
1001
1002         if (APP_STATE_READY != state) {
1003                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state(%d) is NOT 'READY'", uid);
1004                 return TTSD_ERROR_INVALID_STATE;
1005         }
1006
1007         int ret = ttsd_engine_get_private_data(key, data);
1008         if (0 != ret) {
1009                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1010         } else {
1011                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data");
1012         }
1013
1014         return ret;
1015 }
1016
1017 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1018 {
1019         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1020
1021         int ret = 0;
1022         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1023         if (0 != ret) {
1024                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1025         }
1026
1027         return ret;
1028 }
1029
1030 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1031 {
1032         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1033
1034         int ret = 0;
1035         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1036         if (0 != ret) {
1037                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1038         }
1039
1040         return ret;
1041 }
1042
1043 /*
1044 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)
1045 {}
1046
1047 int ttsd_send_error(ttse_error_e error, const char* msg)
1048 {}
1049 */
1050