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