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