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