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