change code for config and change init api and add file for sysinfo check
[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                 /* send message to client about changing state */
630                 ttsdc_send_set_state_message (pid, *uid, APP_STATE_PAUSED);
631                 free(uid);
632         }
633         return EINA_FALSE;
634 }
635
636 int ttsd_server_play(int uid)
637 {
638         app_state_e state;
639         if (0 > ttsd_data_get_client_state(uid, &state)) {
640                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
641                 return TTSD_ERROR_INVALID_PARAMETER;
642         }
643         
644         if (APP_STATE_PLAYING == state) {
645                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
646                 return TTSD_ERROR_NONE;
647         }
648
649         /* check if engine use network */
650         if (ttsd_engine_agent_need_network()) {
651                 if (false == ttsd_network_is_connected()) {
652                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
653                         return TTSD_ERROR_OUT_OF_NETWORK;
654                 }
655         }
656
657         int current_uid = ttsd_data_get_current_playing();
658
659         if (uid != current_uid && -1 != current_uid) {
660                 /* Send interrupt message */
661                 SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
662
663                 /* pause player */
664                 if (0 != ttsd_player_pause(current_uid)) {
665                         SLOG(LOG_WARN, get_tag(), "[Server ERROR] fail to ttsd_player_pause() : uid (%d)", current_uid);
666                 } 
667
668                 /* change state */
669                 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
670
671                 int* temp_uid = (int*)malloc(sizeof(int));
672                 *temp_uid = current_uid;
673                 ecore_timer_add(0, __send_interrupt_client, temp_uid);
674         }
675         
676         /* Change current play */
677         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
678                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
679                 return TTSD_ERROR_OPERATION_FAILED;
680         }
681
682         if (APP_STATE_PAUSED == state) {
683                 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : Next step is resume player and start synthesis ", uid);
684
685                 ttsd_player_state_e state;
686                 if (0 != ttsd_player_get_state(uid, &state)) {
687                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get player state : uid(%d)", uid);
688                         return TTSD_ERROR_OPERATION_FAILED;
689                 }
690
691                 if (TTSD_PLAYER_STATE_PAUSED == state) {
692                         /* Resume player */
693                         if (0 != ttsd_player_resume(uid)) {
694                                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] fail to ttsd_player_resume()");
695                         }
696                 } else if (TTSD_PLAYER_STATE_NULL == state) {
697                         if (0 != ttsd_player_play(uid)) {
698                                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail ttsd_player_play() ");
699                                 
700                                 /* Need to wait synthesis */
701                                 if (NULL == g_wait_timer)
702                                         g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
703                         } else {
704                                 /* success playing */
705                                 SLOG(LOG_DEBUG, get_tag(), "[Server] Success to start player");
706                         }       
707                 } else {
708                         /* error */
709                 }
710         }
711
712         /* Check whether tts-engine is running or not */
713         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
714                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
715         } else {
716                 __synthesis(uid);
717         }
718
719         return TTSD_ERROR_NONE;
720 }
721
722
723 int ttsd_server_stop(int uid)
724 {
725         app_state_e state;
726         if (0 > ttsd_data_get_client_state(uid, &state)) {
727                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid  ");
728                 return TTSD_ERROR_INVALID_PARAMETER;
729         }
730
731         /* Reset all data */
732         ttsd_data_clear_data(uid);
733
734         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
735                 ttsd_data_set_client_state(uid, APP_STATE_READY);
736
737                 if (0 != ttsd_player_stop(uid)) 
738                         SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
739
740                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
741                         SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running ");
742
743                         int ret = 0;
744                         ret = ttsd_engine_cancel_synthesis();
745                         if (0 != ret)
746                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
747                 }
748
749                 if (NULL != g_wait_timer) {
750                         SLOG(LOG_DEBUG, get_tag(), "Wait timer is deleted");
751                         ecore_timer_del(g_wait_timer);
752                 }
753
754                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
755         } else {
756                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is 'ready' ");
757         }
758
759         return TTSD_ERROR_NONE;
760 }
761
762 int ttsd_server_pause(int uid, int* utt_id)
763 {
764         app_state_e state;
765         if (0 > ttsd_data_get_client_state(uid, &state)) {
766                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid  ");
767                 return TTSD_ERROR_INVALID_PARAMETER;
768         }
769
770         if (APP_STATE_PLAYING != state) {
771                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play' ");
772                 return TTSD_ERROR_INVALID_STATE;
773         }
774
775         int ret = 0;
776         ret = ttsd_player_pause(uid);
777         if (0 != ret) {
778                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail player_pause() : ret(%d)", ret);
779                 return TTSD_ERROR_OPERATION_FAILED;
780         }
781
782         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
783
784         return TTSD_ERROR_NONE;
785 }
786
787 int ttsd_server_get_support_voices(int uid, GList** voice_list)
788 {
789         app_state_e state;
790         if (0 > ttsd_data_get_client_state(uid, &state)) {
791                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid  ");
792                 return TTSD_ERROR_INVALID_PARAMETER;
793         }
794
795         /* get voice list*/
796         if (0 != ttsd_engine_get_voice_list(voice_list)) {
797                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() ");
798                 return TTSD_ERROR_OPERATION_FAILED;
799         }
800
801         SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices ");
802
803         return TTSD_ERROR_NONE;
804 }
805
806 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
807 {
808         app_state_e state;
809         if (0 > ttsd_data_get_client_state(uid, &state)) {
810                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid  ");
811                 return TTSD_ERROR_INVALID_PARAMETER;
812         }               
813
814         /* get current voice */
815         int ret = ttsd_engine_get_default_voice(language, (ttsp_voice_type_e*)voice_type);
816         if (0 != ret) {
817                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] fail ttsd_server_get_support_voices() ");
818                 return ret;
819         }
820
821         SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type); 
822
823         return TTSD_ERROR_NONE;
824 }
825
826 #if 0
827 /*
828 * Server API for Internal event
829 */
830 int ttsd_server_start_next_synthesis()
831 {
832         /* get current play */
833         int uid = ttsd_data_is_current_playing();
834
835         if (uid < 0) {
836                 return 0;
837         }
838
839         return __server_next_synthesis(uid);
840 }
841 #endif
842
843 /*
844 * TTS Server Functions for Setting                                                                                                                *
845 */
846
847 int ttsd_server_setting_initialize(int uid)
848 {
849         if (false == g_is_engine) {
850                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
851                         SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] No Engine !!! " );
852                         g_is_engine = false;
853                         return TTSD_ERROR_ENGINE_NOT_FOUND;
854                 } else {
855                         g_is_engine = true;
856                 }
857         }
858
859         if (-1 != ttsd_setting_data_is_setting(uid)) {
860                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] pid has already been registered ");
861                 return TTSD_ERROR_INVALID_PARAMETER;
862         }
863
864         if (0 == ttsd_data_get_client_count()) {
865                 if( 0 != ttsd_engine_agent_load_current_engine() ) {
866                         SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to load current engine ");
867                         return TTSD_ERROR_OPERATION_FAILED;
868                 }
869         }
870
871         /* register pid */
872         if (0 != ttsd_setting_data_add(uid)) {
873                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to add client info ");
874                 return TTSD_ERROR_OPERATION_FAILED;
875         }
876
877         return TTSD_ERROR_NONE;
878 }
879
880 int ttsd_server_setting_finalize(int uid)
881 {
882         if (-1 == ttsd_setting_data_is_setting(uid)) {
883                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
884                 return TTSD_ERROR_INVALID_PARAMETER;
885         }
886
887         ttsd_setting_data_delete(uid);
888
889         /* unload engine, if ref count of client is 0 */
890         if (0 == ttsd_data_get_client_count()) {
891                 ecore_timer_add(0, __quit_ecore_loop, NULL);
892         }
893
894         return TTSD_ERROR_NONE;
895 }
896
897 int ttsd_server_setting_get_engine_list(int uid, GList** engine_list)
898 {
899         if (-1 == ttsd_setting_data_is_setting(uid)) {
900                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
901                 return TTSD_ERROR_INVALID_PARAMETER;
902         }
903
904         int ret = 0;
905         ret = ttsd_engine_setting_get_engine_list(engine_list);
906         if (0 != ret) {
907                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine list : result(%d)", ret);
908                 return ret;
909         }
910
911         return TTSD_ERROR_NONE;
912 }
913
914 int ttsd_server_setting_get_current_engine(int uid, char** engine_id)
915 {
916         if (-1 == ttsd_setting_data_is_setting(uid)) {
917                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
918                 return TTSD_ERROR_INVALID_PARAMETER;
919         }
920
921         int ret = 0;
922         ret = ttsd_engine_setting_get_engine(engine_id);
923         if (0 != ret) {
924                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get current engine : result(%d) ", ret);
925                 return ret;
926         }
927
928         return TTSD_ERROR_NONE;
929 }
930
931 int ttsd_server_setting_set_current_engine(int uid, const char* engine_id)
932 {
933         /* check if uid is valid */
934         if (-1 == ttsd_setting_data_is_setting(uid)) {
935                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
936                 return TTSD_ERROR_INVALID_PARAMETER;
937         }
938
939         if (true == ttsd_engine_agent_is_same_engine(engine_id)) {
940                 SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine ");
941                 return TTSD_ERROR_NONE;
942         }
943
944         /* stop all player */ 
945         ttsd_player_all_stop();
946
947         /* send interrupt message to  all clients */
948         ttsd_data_foreach_clients(__get_client_cb, NULL);
949
950         ttsd_engine_cancel_synthesis();
951
952         /* set engine */
953         int ret = 0;
954         ret = ttsd_engine_setting_set_engine(engine_id);
955         if (0 != ret) {
956                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
957                 return ret;
958         }
959
960         return TTSD_ERROR_NONE;
961 }
962
963 int ttsd_server_setting_get_voice_list(int uid, char** engine_id, GList** voice_list)
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         /* get language list from engine */
972         int ret = 0;
973         ret = ttsd_engine_setting_get_voice_list(engine_id, voice_list);
974         if (0 != ret) {
975                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get voice list : result(%d)", ret);
976                 return ret;
977         }
978
979         return TTSD_ERROR_NONE;
980 }
981
982 int ttsd_server_setting_get_default_voice(int uid, char** language, ttsp_voice_type_e* vctype)
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_default_voice(language, vctype);
992         if (0 != ret) {
993                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] Fail to get default voice : result(%d) ", ret);
994                 return ret;
995         }
996
997         return TTSD_ERROR_NONE;
998 }
999
1000 int ttsd_server_setting_set_default_voice(int uid, const char* language, int vctype)
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         /* set current language */
1009         int ret = 0;
1010         ret = ttsd_engine_setting_set_default_voice((const char*)language, (const ttsp_voice_type_e)vctype);
1011         if (0 != ret) {
1012                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default voice : result(%d) ", ret);
1013                 return ret;
1014         }       
1015
1016         return TTSD_ERROR_NONE;
1017 }
1018
1019 int ttsd_server_setting_get_engine_setting(int uid, char** engine_id, GList** engine_setting_list)
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         int ret = 0;
1028         ret = ttsd_engine_setting_get_engine_setting_info(engine_id, engine_setting_list);
1029         if (0 != ret) {
1030                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get engine setting info : result(%d)", ret);
1031                 return ret;
1032         }
1033
1034         return TTSD_ERROR_NONE;
1035 }
1036
1037 int ttsd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
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         int ret = 0;
1046         ret = ttsd_engine_setting_set_engine_setting(key, value);
1047         if (0 != ret) {
1048                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set engine setting info : result(%d)", ret);
1049                 return ret;
1050         }
1051
1052         return TTSD_ERROR_NONE;
1053 }
1054
1055 int ttsd_server_setting_get_default_speed(int uid, int* default_speed)
1056 {
1057         /* check if uid is valid */
1058         if (-1 == ttsd_setting_data_is_setting(uid)) {
1059                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] uid is not valid  (%s)", uid);
1060                 return TTSD_ERROR_INVALID_PARAMETER;
1061         }
1062
1063         /* get current speed */
1064         int ret = 0;
1065         ret = ttsd_engine_setting_get_default_speed((ttsp_speed_e*)default_speed);
1066         if (0 != ret) {
1067                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to get default speed : result(%d)", ret);
1068                 return ret;
1069         }       
1070
1071         return TTSD_ERROR_NONE;
1072 }
1073
1074 int ttsd_server_setting_set_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         /* set default speed */
1083         int ret = 0;
1084         ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)default_speed);
1085         if (0 != ret) {
1086                 SLOG(LOG_ERROR, get_tag(), "[Server Setting ERROR] fail to set default speed : result(%d)", ret);
1087                 return ret;
1088         }       
1089
1090         return TTSD_ERROR_NONE;
1091 }