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