Fix to remove timer when daemon is destroyed
[platform/core/uifw/tts.git] / server / ttsd_server.c
1 /*
2 *  Copyright (c) 2012, 2013 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 <Ecore.h>
15 #include "ttsd_main.h"
16 #include "ttsd_player.h"
17 #include "ttsd_data.h"
18 #include "ttsd_engine_agent.h"
19 #include "ttsd_server.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsp.h"
22 #include "ttsd_dbus.h"
23 #include "ttsd_config.h"
24 #include "ttsd_network.h"
25
26 typedef enum {
27         TTSD_SYNTHESIS_CONTROL_DOING    = 0,
28         TTSD_SYNTHESIS_CONTROL_DONE     = 1,
29         TTSD_SYNTHESIS_CONTROL_EXPIRED  = 2
30 }ttsd_synthesis_control_e;
31
32 typedef struct {
33         int uid;
34         int uttid;
35 }utterance_t;
36
37 /* If current engine exist */
38 static bool     g_is_engine;
39
40 /* If engine is running */
41 static ttsd_synthesis_control_e g_synth_control;
42
43 static Ecore_Timer* g_wait_timer = NULL;
44
45 /* Function definitions */
46 int __server_next_synthesis(int uid);
47
48
49 int __server_set_synth_control(ttsd_synthesis_control_e control)
50 {
51         g_synth_control = control;
52         return 0;
53 }
54
55 ttsd_synthesis_control_e __server_get_synth_control()
56 {
57         return g_synth_control;
58 }
59
60 int __server_send_error(int uid, int utt_id, int error_code)
61 {
62         int pid = ttsd_data_get_pid(uid);
63
64         /* send error */
65         if ( 0 != ttsdc_send_error_message(pid, uid, utt_id, error_code)) {
66                 ttsd_data_delete_client(uid);                   
67         } 
68         
69         return 0;
70 }
71
72 Eina_Bool __wait_synthesis(void *data)
73 {
74         /* get current play */
75         int uid = ttsd_data_is_current_playing();
76
77         if (uid > 0) {
78                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
79                         return EINA_TRUE;
80                 } else {
81                         g_wait_timer = NULL;
82                         if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
83                                 /* Start next synthesis */
84                                 __server_next_synthesis(uid);
85                         }
86                 }       
87         } else {
88                 g_wait_timer = NULL;
89         }
90
91         return EINA_FALSE;
92 }
93
94 int __synthesis(int uid)
95 {
96         speak_data_s sdata;
97         if (0 == ttsd_data_get_speak_data(uid, &sdata)) {
98
99                 utterance_t* utt = (utterance_t*)g_malloc0(sizeof(utterance_t));
100
101                 if (NULL == utt) {
102                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] fail to allocate memory : utterance ", __FUNCTION__);
103                         return TTSD_ERROR_OUT_OF_MEMORY;
104                 }
105
106                 utt->uid = uid;
107                 utt->uttid = sdata.utt_id;
108
109                 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
110                 SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", utt->uid, utt->uttid );
111                 SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", sdata.lang, sdata.vctype, sdata.speed);
112                 SLOG(LOG_DEBUG, get_tag(), "Text : %s", sdata.text);
113                 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
114
115                 int ret = 0;
116                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
117                 ret = ttsd_engine_start_synthesis(sdata.lang, sdata.vctype, sdata.text, sdata.speed, (void*)utt);
118                 if (0 != ret) {
119                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR][%s] * FAIL to start SYNTHESIS !!!! * ", __FUNCTION__);
120
121                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
122
123                         ttsd_config_save_error(utt->uid, utt->uttid, sdata.lang, sdata.vctype, sdata.text, __FUNCTION__, __LINE__, "fail to start synthesis");
124
125                         g_free(utt);
126
127                         ttsd_server_stop(uid);
128
129                         int pid = ttsd_data_get_pid(uid);
130                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
131                 } else {
132                         g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
133                 }
134
135                 if(sdata.text != NULL)  
136                         g_free(sdata.text);
137         } else {
138                 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
139                 SLOG(LOG_DEBUG, get_tag(), "[Server] Text queue is empty.");
140                 SLOG(LOG_DEBUG, get_tag(), "[Server] --------------------");
141         }
142
143         return 0;
144 }
145
146 int __server_next_synthesis(int uid)
147 {
148         SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY START");
149
150         /* get current playing client */
151         int current_uid = ttsd_data_get_current_playing();
152
153         if (0 > current_uid) {
154                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current uid is not valid");
155                 SLOG(LOG_DEBUG, get_tag(), "=====");
156                 SLOG(LOG_DEBUG, get_tag(), "  ");
157                 return 0;
158         }
159
160         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
161                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running. ");
162                 SLOG(LOG_DEBUG, get_tag(), "=====");
163                 SLOG(LOG_DEBUG, get_tag(), "  ");
164                 return 0;
165         }
166
167         __synthesis(current_uid);
168
169         if (0 != ttsd_player_play(current_uid)) {
170                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail ttsd_player_play() ");
171         } else {
172                 /* success playing */
173                 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
174         }
175
176         SLOG(LOG_DEBUG, get_tag(), "===== NEXT SYNTHESIS & PLAY END");
177         SLOG(LOG_DEBUG, get_tag(), "  ");
178
179         return 0;
180 }
181
182 /*
183 * TTS Server Callback Functions 
184 */
185
186 int __player_result_callback(player_event_e event, int uid, int utt_id)
187 {
188         switch(event) {
189         case PLAYER_ERROR:
190                 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR][%s] player result error", __FUNCTION__);
191                 __server_send_error(uid, utt_id, TTSD_ERROR_OPERATION_FAILED);
192                 ttsd_config_save_error(uid, utt_id, NULL, -1, NULL, __FUNCTION__, __LINE__, "PLAYER_ERROR");
193
194         case PLAYER_EMPTY_SOUND_QUEUE:
195                 /* check whether synthesis is running */
196                 if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
197                         /* check text queue is empty */
198                         if (0 == ttsd_data_get_speak_data_size(uid) && 0 == ttsd_data_get_sound_data_size(uid)) {
199                                 SLOG(LOG_DEBUG, get_tag(), "[SERVER Callback] all play completed ");
200                         }
201                 } 
202                 break;
203         
204         case PLAYER_END_OF_PLAYING:
205                 break;
206         }
207
208         return 0;
209 }
210
211 int __synthesis_result_callback(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data)
212 {
213         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK START");
214
215         utterance_t* utt_get_param;
216         utt_get_param = (utterance_t*)user_data;
217
218         if (NULL == utt_get_param) {
219                 SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] User data is NULL " );
220                 SLOG(LOG_DEBUG, get_tag(), "=====");
221                 SLOG(LOG_DEBUG, get_tag(), "  ");
222                 return -1;
223         }
224
225         int uid = utt_get_param->uid;
226         int uttid = utt_get_param->uttid;
227
228         /* Synthesis is success */
229         if (TTSP_RESULT_EVENT_START == event || TTSP_RESULT_EVENT_CONTINUE == event || TTSP_RESULT_EVENT_FINISH == event) {
230                 
231                 if (TTSP_RESULT_EVENT_START == event)           SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_START");
232                 if (TTSP_RESULT_EVENT_CONTINUE == event)        SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CONTINUE");
233                 if (TTSP_RESULT_EVENT_FINISH == event)          SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_FINISH");
234
235                 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
236                         SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
237                         SLOG(LOG_DEBUG, get_tag(), "=====");
238                         SLOG(LOG_DEBUG, get_tag(), "  ");
239
240                         return 0;
241                 }
242
243                 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) ", 
244                         uid, uttid, data, data_size);
245
246                 /* add wav data */
247                 sound_data_s temp_data;
248                 temp_data.data = (char*)g_malloc0( sizeof(char) * data_size );
249                 memcpy(temp_data.data, data, data_size);
250
251                 temp_data.data_size = data_size;
252                 temp_data.utt_id = utt_get_param->uttid;
253                 temp_data.event = event;
254
255                 ttsp_audio_type_e audio_type;
256                 int rate;
257                 int channels;
258
259                 if (ttsd_engine_get_audio_format(&audio_type, &rate, &channels)) {
260                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get audio format ");
261                         SLOG(LOG_DEBUG, get_tag(), "=====");
262                         SLOG(LOG_DEBUG, get_tag(), "  ");
263                         return -1;
264                 }
265                 
266                 temp_data.audio_type = audio_type;
267                 temp_data.rate = rate;
268                 temp_data.channels = channels;
269                 
270                 if (0 != ttsd_data_add_sound_data(uid, temp_data)) {
271                         SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", utt_get_param->uid);
272                 }
273
274                 if (event == TTSP_RESULT_EVENT_FINISH) {
275                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
276                 }
277         } else if (event == TTSP_RESULT_EVENT_CANCEL) {
278                 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CANCEL");
279                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
280         } else {
281                 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_ERROR");
282                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
283         } 
284
285         if (TTSP_RESULT_EVENT_FINISH == event || TTSP_RESULT_EVENT_CANCEL == event || TTSP_RESULT_EVENT_FAIL == event) {
286                 if (NULL != utt_get_param)              
287                         free(utt_get_param);
288         }
289
290         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK END");
291         SLOG(LOG_DEBUG, get_tag(), "  ");
292
293 #if 0
294         if (true == __server_get_is_next_synthesis()) {
295                 __server_set_is_next_synthesis(false);
296
297                 /* Do NOT work ecore timer because of This function is thread callbacked */ 
298                 /* need to send dbus message event */
299                 ttsd_send_start_next_synthesis();
300         }
301 #endif
302
303         return 0;
304 }
305
306 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
307 {
308         /* clear client data */
309         ttsd_data_clear_data(uid);                      
310         ttsd_data_set_client_state(uid, APP_STATE_READY);
311
312         /* send message */
313         if ( 0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
314                 /* remove client */
315                 ttsd_data_delete_client(uid);
316         } 
317
318         return true;
319 }
320
321 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
322 {
323         switch(type) {
324         case TTS_CONFIG_TYPE_ENGINE:
325         {
326                 if (NULL == str_param) {
327                         SLOG(LOG_ERROR, get_tag(), "[Server] engine id from config is NULL");
328                         return;
329                 }
330
331                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
332                         SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
333                         return;
334                 }
335
336                 /* stop all player */ 
337                 ttsd_player_all_stop();
338
339                 /* send interrupt message to  all clients */
340                 ttsd_data_foreach_clients(__get_client_cb, NULL);
341
342                 ttsd_engine_cancel_synthesis();
343
344                 /* set engine */
345                 int ret = 0;
346                 ret = ttsd_engine_setting_set_engine(str_param);
347                 if (0 != ret) {
348                         SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
349                 }
350
351                 break;
352         }
353                 
354         case TTS_CONFIG_TYPE_VOICE:
355         {
356                 if (NULL == str_param) {
357                         SLOG(LOG_ERROR, get_tag(), "[Server] language from config is NULL");
358                         return;
359                 }
360
361                 char* out_lang;
362                 ttsp_voice_type_e out_type;
363                 int ret = -1;
364
365                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
366                         SLOG(LOG_ERROR, get_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
367                         ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
368                         if (0 != ret)
369                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
370
371                         if (NULL == out_lang)
372                                 free(out_lang);
373                 } else {
374                         /* Current language is not available */
375                         if (true == ttsd_engine_select_valid_voice("en_US", 2, &out_lang, &out_type)) {
376                                 ret = ttsd_engine_setting_set_default_voice(out_lang, out_type);
377                                 if (0 != ret) 
378                                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
379
380                                 if (NULL == out_lang)
381                                         free(out_lang);
382                         }
383                 }
384                 break;
385         }
386         
387         case TTS_CONFIG_TYPE_SPEED:
388         {
389                 if (TTSP_SPEED_VERY_SLOW <= int_param && int_param <= TTSP_SPEED_VERY_FAST) {
390                         /* set default speed */
391                         int ret = 0;
392                         ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)int_param);
393                         if (0 != ret) {
394                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail to set default speed : result(%d)", ret);
395                         }       
396                 }
397                 break;
398         }
399                 
400         default:
401                 break;
402         }
403         
404         return;
405 }
406
407
408 /*
409 * Server APIs
410 */
411
412 int ttsd_initialize()
413 {
414         if (ttsd_config_initialize(__config_changed_cb)) {
415                 SLOG(LOG_ERROR, get_tag(), "[Server WARNING] Fail to initialize config.");
416         }
417
418         /* player init */
419         if (ttsd_player_init(__player_result_callback)) {
420                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to initialize player init.");
421                 return TTSD_ERROR_OPERATION_FAILED;
422         }
423
424         /* Engine Agent initialize */
425         if (0 != ttsd_engine_agent_init(__synthesis_result_callback)) {
426                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to engine agent initialize.");
427                 return TTSD_ERROR_OPERATION_FAILED;
428         }
429
430         /* set current engine */
431         if (0 != ttsd_engine_agent_initialize_current_engine()) {
432                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!" );
433                 g_is_engine = false;
434         } else 
435                 g_is_engine = true;
436         
437         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
438
439         return TTSD_ERROR_NONE;
440 }
441
442 int ttsd_finalize()
443 {
444         ttsd_config_finalize();
445         
446         ttsd_player_release();
447
448         ttsd_engine_agent_release();
449
450         return TTSD_ERROR_NONE;
451 }
452
453 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
454 {
455         int result = 1;
456
457         result = ttsdc_send_hello(pid, uid);
458
459         if (0 == result) {
460                 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid); 
461                 ttsd_server_finalize(uid);
462         } else if (-1 == result) {
463                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error"); 
464         }
465
466         return true;
467 }
468
469
470 Eina_Bool ttsd_cleanup_client(void *data)
471 {
472         SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
473         ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
474         SLOG(LOG_DEBUG, get_tag(), "=====");
475         SLOG(LOG_DEBUG, get_tag(), "  ");
476
477         return EINA_TRUE;
478 }
479
480 /*
481 * TTS Server Functions for Client
482 */
483
484 int ttsd_server_initialize(int pid, int uid)
485 {
486         if (false == g_is_engine) {
487                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
488                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!! " );
489                         g_is_engine = false;
490
491                         return TTSD_ERROR_ENGINE_NOT_FOUND;
492                 } else {
493                         g_is_engine = true;
494                 }
495         }
496
497         if (-1 != ttsd_data_is_client(uid)) {
498                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Uid has already been registered ");
499                 return TTSD_ERROR_INVALID_PARAMETER;
500         }
501
502         if (0 == ttsd_data_get_client_count()) {
503                 if (0 != ttsd_engine_agent_load_current_engine()) {
504                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine ");
505                         return TTSD_ERROR_OPERATION_FAILED;
506                 }
507                 /* Check system language */
508                 ttsd_config_update_language();
509         }
510
511         if (0 == ttsd_data_get_same_pid_client_count(pid)) {
512                 SLOG(LOG_DEBUG, get_tag(), "[Server] open file msg connection");
513                 if (0 != ttsd_file_msg_open_connection(pid)) {
514                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail to open file message connection");
515                         return TTSD_ERROR_OPERATION_FAILED;
516                 }
517         }
518
519         if (0 != ttsd_data_new_client(pid, uid)) {
520                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info ");
521                 return TTSD_ERROR_OPERATION_FAILED;
522         }
523
524         if (0 != ttsd_player_create_instance(uid)) {
525                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player ");
526                 return TTSD_ERROR_OPERATION_FAILED;
527         }
528
529         return TTSD_ERROR_NONE;
530 }
531
532 static Eina_Bool __quit_ecore_loop(void *data)
533 {
534         ecore_main_loop_quit();
535         SLOG(LOG_DEBUG, get_tag(), "[Server] quit ecore main loop");
536         return EINA_FALSE;
537 }
538
539 int ttsd_server_finalize(int uid)
540 {
541         app_state_e state;
542         if (0 > ttsd_data_get_client_state(uid, &state)) {
543                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid  ");
544                 return TTSD_ERROR_INVALID_PARAMETER;
545         }
546
547         ttsd_server_stop(uid);
548         
549         ttsd_player_destroy_instance(uid);
550
551         int pid = ttsd_data_get_pid(uid);
552
553         ttsd_data_delete_client(uid);
554
555         if (0 == ttsd_data_get_same_pid_client_count(pid)) {
556                 SLOG(LOG_DEBUG, get_tag(), "[Sever] File msg close connection");
557                 ttsd_file_msg_close_connection(pid);
558         }
559
560         /* unload engine, if ref count of client is 0 */
561         if (0 == ttsd_data_get_client_count()) {
562                 ecore_timer_add(0, __quit_ecore_loop, NULL);
563         }
564
565         return TTSD_ERROR_NONE;
566 }
567
568 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
569 {
570         app_state_e state;
571         if (0 > ttsd_data_get_client_state(uid, &state)) {
572                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid  ");
573                 return TTSD_ERROR_INVALID_PARAMETER;
574         }
575
576         /* check valid voice */
577         char* temp_lang = NULL;
578         ttsp_voice_type_e temp_type;
579         if (true != ttsd_engine_select_valid_voice((const char*)lang, (const ttsp_voice_type_e)voice_type, &temp_lang, &temp_type)) {
580                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice ");
581                 return TTSD_ERROR_INVALID_VOICE;
582         } else {                
583                 if (NULL == temp_lang)
584                         free(temp_lang);
585         }
586         
587         speak_data_s data;
588
589         data.lang = strdup(lang);
590         data.vctype = (ttsp_voice_type_e)voice_type;
591
592         data.speed = (ttsp_speed_e)speed;
593         data.utt_id = utt_id;
594                 
595         data.text = strdup(text);
596
597         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
598         if (0 != ttsd_data_add_speak_data(uid, data)) {
599                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : Current state of uid is not 'ready' ");
600                 return TTSD_ERROR_OPERATION_FAILED;
601         }
602
603         if (APP_STATE_PLAYING == state) {
604                 /* check if engine use network */
605                 if (ttsd_engine_agent_need_network()) {
606                         if (false == ttsd_network_is_connected()) {
607                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
608                                 return TTSD_ERROR_OPERATION_FAILED;
609                         }
610                 }
611
612                 /* Check whether tts-engine is running or not */
613                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
614                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
615                 } else {
616                         __synthesis(uid);
617                 }
618         }
619
620         return TTSD_ERROR_NONE;
621 }
622
623 Eina_Bool __send_interrupt_client(void *data)
624 {
625         int* uid = (int*)data;
626         
627         if (NULL != uid) {
628                 int pid = ttsd_data_get_pid(*uid);
629
630                 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
631                         /* send message to client about changing state */
632                         ttsdc_send_set_state_message (pid, *uid, APP_STATE_READY);
633                 } else {
634                         ttsdc_send_set_state_message (pid, *uid, APP_STATE_PAUSED);
635                 }
636                 free(uid);
637         }       
638         return EINA_FALSE;
639 }
640
641 int ttsd_server_play(int uid)
642 {
643         app_state_e state;
644         if (0 > ttsd_data_get_client_state(uid, &state)) {
645                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
646                 return TTSD_ERROR_INVALID_PARAMETER;
647         }
648         
649         if (APP_STATE_PLAYING == state) {
650                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
651                 return TTSD_ERROR_NONE;
652         }
653
654         /* check if engine use network */
655         if (ttsd_engine_agent_need_network()) {
656                 if (false == ttsd_network_is_connected()) {
657                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
658                         return TTSD_ERROR_OUT_OF_NETWORK;
659                 }
660         }
661
662         int current_uid = ttsd_data_get_current_playing();
663
664         if (uid != current_uid && -1 != current_uid) {
665                 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
666                         /* Send interrupt message */
667                         SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
668
669                         /* pause player */
670                         if (0 != ttsd_server_stop(current_uid)) {
671                                 SLOG(LOG_WARN, get_tag(), "[Server ERROR] fail to stop : uid (%d)", current_uid);
672                         } 
673                         
674                         int* temp_uid = (int*)malloc(sizeof(int));
675                         *temp_uid = current_uid;
676                         ecore_timer_add(0, __send_interrupt_client, temp_uid);
677                 } else {
678                         /* Send interrupt message */
679                         SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
680
681                         /* pause player */
682                         if (0 != ttsd_player_pause(current_uid)) {
683                                 SLOG(LOG_WARN, get_tag(), "[Server ERROR] fail to ttsd_player_pause() : uid (%d)", current_uid);
684                         } 
685
686                         /* change state */
687                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
688
689                         int* temp_uid = (int*)malloc(sizeof(int));
690                         *temp_uid = current_uid;
691                         ecore_timer_add(0, __send_interrupt_client, temp_uid);
692                 }
693         }
694         
695         /* Change current play */
696         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
697                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
698                 return TTSD_ERROR_OPERATION_FAILED;
699         }
700
701         if (APP_STATE_PAUSED == state) {
702                 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : Next step is resume player and start synthesis ", uid);
703
704                 ttsd_player_state_e state;
705                 if (0 != ttsd_player_get_state(uid, &state)) {
706                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get player state : uid(%d)", uid);
707                         return TTSD_ERROR_OPERATION_FAILED;
708                 }
709
710                 if (TTSD_PLAYER_STATE_PAUSED == state) {
711                         /* Resume player */
712                         if (0 != ttsd_player_resume(uid)) {
713                                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] fail to ttsd_player_resume()");
714                         }
715                 } else if (TTSD_PLAYER_STATE_NULL == state) {
716                         if (0 != ttsd_player_play(uid)) {
717                                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail ttsd_player_play() ");
718                                 
719                                 /* Need to wait synthesis */
720                                 if (NULL == g_wait_timer)
721                                         g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
722                         } else {
723                                 /* success playing */
724                                 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
725                         }       
726                 } else {
727                         /* error */
728                 }
729         }
730
731         /* Check whether tts-engine is running or not */
732         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
733                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
734         } else {
735                 __synthesis(uid);
736         }
737
738         return TTSD_ERROR_NONE;
739 }
740
741
742 int ttsd_server_stop(int uid)
743 {
744         app_state_e state;
745         if (0 > ttsd_data_get_client_state(uid, &state)) {
746                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid  ");
747                 return TTSD_ERROR_INVALID_PARAMETER;
748         }
749
750         /* Reset all data */
751         ttsd_data_clear_data(uid);
752
753         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
754                 ttsd_data_set_client_state(uid, APP_STATE_READY);
755
756                 if (0 != ttsd_player_stop(uid)) 
757                         SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
758
759                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
760                         SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running ");
761
762                         int ret = 0;
763                         ret = ttsd_engine_cancel_synthesis();
764                         if (0 != ret)
765                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
766                 }
767
768                 if (NULL != g_wait_timer) {
769                         SLOG(LOG_DEBUG, get_tag(), "Wait timer is deleted");
770                         ecore_timer_del(g_wait_timer);
771                 }
772
773                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
774         } else {
775                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is 'ready' ");
776         }
777
778         return TTSD_ERROR_NONE;
779 }
780
781 int ttsd_server_pause(int uid, int* utt_id)
782 {
783         app_state_e state;
784         if (0 > ttsd_data_get_client_state(uid, &state)) {
785                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid  ");
786                 return TTSD_ERROR_INVALID_PARAMETER;
787         }
788
789         if (APP_STATE_PLAYING != state) {
790                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play' ");
791                 return TTSD_ERROR_INVALID_STATE;
792         }
793
794         int ret = 0;
795         ret = ttsd_player_pause(uid);
796         if (0 != ret) {
797                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail player_pause() : ret(%d)", ret);
798                 return TTSD_ERROR_OPERATION_FAILED;
799         }
800
801         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
802
803         return TTSD_ERROR_NONE;
804 }
805
806 int ttsd_server_get_support_voices(int uid, GList** voice_list)
807 {
808         app_state_e state;
809         if (0 > ttsd_data_get_client_state(uid, &state)) {
810                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid  ");
811                 return TTSD_ERROR_INVALID_PARAMETER;
812         }
813
814         /* get voice list*/
815         if (0 != ttsd_engine_get_voice_list(voice_list)) {
816                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() ");
817                 return TTSD_ERROR_OPERATION_FAILED;
818         }
819
820         SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices ");
821
822         return TTSD_ERROR_NONE;
823 }
824
825 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
826 {
827         app_state_e state;
828         if (0 > ttsd_data_get_client_state(uid, &state)) {
829                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid  ");
830                 return TTSD_ERROR_INVALID_PARAMETER;
831         }               
832
833         /* get current voice */
834         int ret = ttsd_engine_get_default_voice(language, (ttsp_voice_type_e*)voice_type);
835         if (0 != ret) {
836                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail ttsd_server_get_support_voices() ");
837                 return ret;
838         }
839
840         SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type); 
841
842         return TTSD_ERROR_NONE;
843 }
844
845 #if 0
846 /*
847 * Server API for Internal event
848 */
849 int ttsd_server_start_next_synthesis()
850 {
851         /* get current play */
852         int uid = ttsd_data_is_current_playing();
853
854         if (uid < 0) {
855                 return 0;
856         }
857
858         return __server_next_synthesis(uid);
859 }
860 #endif
861
862 /*
863 * TTS Server Functions for Setting                                                                                                                *
864 */
865
866 int ttsd_server_setting_initialize(int uid)
867 {
868         if (false == g_is_engine) {
869                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
870                         SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] No Engine !!! " );
871                         g_is_engine = false;
872                         return TTSD_ERROR_ENGINE_NOT_FOUND;
873                 } else {
874                         g_is_engine = true;
875                 }
876         }
877
878         if (-1 != ttsd_setting_data_is_setting(uid)) {
879                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] pid has already been registered ");
880                 return TTSD_ERROR_INVALID_PARAMETER;
881         }
882
883         if (0 == ttsd_data_get_client_count()) {
884                 if( 0 != ttsd_engine_agent_load_current_engine() ) {
885                         SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to load current engine ");
886                         return TTSD_ERROR_OPERATION_FAILED;
887                 }
888         }
889
890         /* register pid */
891         if (0 != ttsd_setting_data_add(uid)) {
892                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to add client info ");
893                 return TTSD_ERROR_OPERATION_FAILED;
894         }
895
896         return TTSD_ERROR_NONE;
897 }
898
899 int ttsd_server_setting_finalize(int uid)
900 {
901         if (-1 == ttsd_setting_data_is_setting(uid)) {
902                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
903                 return TTSD_ERROR_INVALID_PARAMETER;
904         }
905
906         ttsd_setting_data_delete(uid);
907
908         /* unload engine, if ref count of client is 0 */
909         if (0 == ttsd_data_get_client_count()) {
910                 ecore_timer_add(0, __quit_ecore_loop, NULL);
911         }
912
913         return TTSD_ERROR_NONE;
914 }
915
916 int ttsd_server_setting_get_engine_list(int uid, GList** engine_list)
917 {
918         if (-1 == ttsd_setting_data_is_setting(uid)) {
919                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
920                 return TTSD_ERROR_INVALID_PARAMETER;
921         }
922
923         int ret = 0;
924         ret = ttsd_engine_setting_get_engine_list(engine_list);
925         if (0 != ret) {
926                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine list : result(%d)", ret);
927                 return ret;
928         }
929
930         return TTSD_ERROR_NONE;
931 }
932
933 int ttsd_server_setting_get_current_engine(int uid, char** engine_id)
934 {
935         if (-1 == ttsd_setting_data_is_setting(uid)) {
936                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
937                 return TTSD_ERROR_INVALID_PARAMETER;
938         }
939
940         int ret = 0;
941         ret = ttsd_engine_setting_get_engine(engine_id);
942         if (0 != ret) {
943                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get current engine : result(%d) ", ret);
944                 return ret;
945         }
946
947         return TTSD_ERROR_NONE;
948 }
949
950 int ttsd_server_setting_set_current_engine(int uid, const char* engine_id)
951 {
952         /* check if uid is valid */
953         if (-1 == ttsd_setting_data_is_setting(uid)) {
954                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
955                 return TTSD_ERROR_INVALID_PARAMETER;
956         }
957
958         if (true == ttsd_engine_agent_is_same_engine(engine_id)) {
959                 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
960                 return TTSD_ERROR_NONE;
961         }
962
963         /* stop all player */ 
964         ttsd_player_all_stop();
965
966         /* send interrupt message to  all clients */
967         ttsd_data_foreach_clients(__get_client_cb, NULL);
968
969         ttsd_engine_cancel_synthesis();
970
971         /* set engine */
972         int ret = 0;
973         ret = ttsd_engine_setting_set_engine(engine_id);
974         if (0 != ret) {
975                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
976                 return ret;
977         }
978
979         return TTSD_ERROR_NONE;
980 }
981
982 int ttsd_server_setting_get_voice_list(int uid, char** engine_id, GList** voice_list)
983 {
984         /* check if uid is valid */
985         if (-1 == ttsd_setting_data_is_setting(uid)) {
986                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
987                 return TTSD_ERROR_INVALID_PARAMETER;
988         }
989
990         /* get language list from engine */
991         int ret = 0;
992         ret = ttsd_engine_setting_get_voice_list(engine_id, voice_list);
993         if (0 != ret) {
994                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get voice list : result(%d)", ret);
995                 return ret;
996         }
997
998         return TTSD_ERROR_NONE;
999 }
1000
1001 int ttsd_server_setting_get_default_voice(int uid, char** language, ttsp_voice_type_e* vctype)
1002 {
1003         /* check if uid is valid */
1004         if (-1 == ttsd_setting_data_is_setting(uid)) {
1005                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1006                 return TTSD_ERROR_INVALID_PARAMETER;
1007         }
1008         
1009         int ret = 0;
1010         ret = ttsd_engine_setting_get_default_voice(language, vctype);
1011         if (0 != ret) {
1012                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get default voice : result(%d) ", ret);
1013                 return ret;
1014         }
1015
1016         return TTSD_ERROR_NONE;
1017 }
1018
1019 int ttsd_server_setting_set_default_voice(int uid, const char* language, int vctype)
1020 {
1021         /* check if uid is valid */
1022         if (-1 == ttsd_setting_data_is_setting(uid)) {
1023                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1024                 return TTSD_ERROR_INVALID_PARAMETER;
1025         }
1026
1027         /* set current language */
1028         int ret = 0;
1029         ret = ttsd_engine_setting_set_default_voice((const char*)language, (const ttsp_voice_type_e)vctype);
1030         if (0 != ret) {
1031                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default voice : result(%d) ", ret);
1032                 return ret;
1033         }       
1034
1035         return TTSD_ERROR_NONE;
1036 }
1037
1038 int ttsd_server_setting_get_engine_setting(int uid, char** engine_id, GList** engine_setting_list)
1039 {
1040         /* check if uid is valid */
1041         if (-1 == ttsd_setting_data_is_setting(uid)) {
1042                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1043                 return TTSD_ERROR_INVALID_PARAMETER;
1044         }
1045
1046         int ret = 0;
1047         ret = ttsd_engine_setting_get_engine_setting_info(engine_id, engine_setting_list);
1048         if (0 != ret) {
1049                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine setting info : result(%d)", ret);
1050                 return ret;
1051         }
1052
1053         return TTSD_ERROR_NONE;
1054 }
1055
1056 int ttsd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
1057 {
1058         /* check if uid is valid */
1059         if (-1 == ttsd_setting_data_is_setting(uid)) {
1060                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1061                 return TTSD_ERROR_INVALID_PARAMETER;
1062         }
1063
1064         int ret = 0;
1065         ret = ttsd_engine_setting_set_engine_setting(key, value);
1066         if (0 != ret) {
1067                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set engine setting info : result(%d)", ret);
1068                 return ret;
1069         }
1070
1071         return TTSD_ERROR_NONE;
1072 }
1073
1074 int ttsd_server_setting_get_default_speed(int uid, int* default_speed)
1075 {
1076         /* check if uid is valid */
1077         if (-1 == ttsd_setting_data_is_setting(uid)) {
1078                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1079                 return TTSD_ERROR_INVALID_PARAMETER;
1080         }
1081
1082         /* get current speed */
1083         int ret = 0;
1084         ret = ttsd_engine_setting_get_default_speed((ttsp_speed_e*)default_speed);
1085         if (0 != ret) {
1086                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get default speed : result(%d)", ret);
1087                 return ret;
1088         }       
1089
1090         return TTSD_ERROR_NONE;
1091 }
1092
1093 int ttsd_server_setting_set_default_speed(int uid, int default_speed)
1094 {
1095         /* check if uid is valid */
1096         if (-1 == ttsd_setting_data_is_setting(uid)) {
1097                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1098                 return TTSD_ERROR_INVALID_PARAMETER;
1099         }
1100
1101         /* set default speed */
1102         int ret = 0;
1103         ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)default_speed);
1104         if (0 != ret) {
1105                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default speed : result(%d)", ret);
1106                 return ret;
1107         }       
1108
1109         return TTSD_ERROR_NONE;
1110 }