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