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