Merge with Tizen 2.3
[platform/core/uifw/tts.git] / server / ttsd_server.c
1 /*
2 *  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <aul.h>
15 #include <Ecore.h>
16
17 #include "ttsd_config.h"
18 #include "ttsd_data.h"
19 #include "ttsd_dbus.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_engine_agent.h"
22 #include "ttsd_main.h"
23 #include "ttsd_network.h"
24 #include "ttsd_player.h"
25 #include "ttsd_server.h"
26 #include "ttsp.h"
27
28
29 typedef enum {
30         TTSD_SYNTHESIS_CONTROL_DOING    = 0,
31         TTSD_SYNTHESIS_CONTROL_DONE     = 1,
32         TTSD_SYNTHESIS_CONTROL_EXPIRED  = 2
33 }ttsd_synthesis_control_e;
34
35 typedef struct {
36         int uid;
37         int uttid;
38 }utterance_t;
39
40 /* If current engine exist */
41 static bool     g_is_engine;
42
43 /* If engine is running */
44 static ttsd_synthesis_control_e g_synth_control;
45
46 static Ecore_Timer* g_wait_timer = NULL;
47
48 static utterance_t g_utt;
49
50 /* Function definitions */
51 static int __synthesis(int uid);
52
53 static int __server_set_synth_control(ttsd_synthesis_control_e control)
54 {
55         g_synth_control = control;
56         return 0;
57 }
58
59 static ttsd_synthesis_control_e __server_get_synth_control()
60 {
61         return g_synth_control;
62 }
63
64 static Eina_Bool __wait_synthesis(void *data)
65 {
66         /* get current play */
67         int uid = ttsd_data_get_current_playing();
68
69         if (uid > 0) {
70                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
71                         usleep(100000);
72                         return EINA_TRUE;
73                 } else {
74                         g_wait_timer = NULL;
75                         if (TTSD_SYNTHESIS_CONTROL_DONE == __server_get_synth_control()) {
76                                 /* Start next synthesis */
77                                 __synthesis(uid);
78                         }
79                 }       
80         } else {
81                 g_wait_timer = NULL;
82         }
83
84         return EINA_FALSE;
85 }
86
87 static int __synthesis(int uid)
88 {
89         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS  START");
90
91         speak_data_s sdata;
92         if (0 == ttsd_data_get_speak_data(uid, &sdata)) {
93
94                 if (NULL == sdata.lang || NULL == sdata.text) {
95                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Current data is NOT valid");
96                         ttsd_server_stop(uid);
97                         
98                         int pid = ttsd_data_get_pid(uid);
99                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
100
101                         if (NULL != sdata.lang) free(sdata.lang);
102                         
103                         return 0;
104                 }
105
106                 g_utt.uid = uid;
107                 g_utt.uttid = sdata.utt_id;
108
109                 SLOG(LOG_DEBUG, get_tag(), "-----------------------------------------------------------");
110                 SECURE_SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_utt.uttid);
111                 SECURE_SLOG(LOG_DEBUG, get_tag(), "Voice : langauge(%s), type(%d), speed(%d)", sdata.lang, sdata.vctype, sdata.speed);
112                 SECURE_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, NULL);
118                 if (0 != ret) {
119                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
120
121                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
122
123                         ttsd_server_stop(uid);
124
125                         int pid = ttsd_data_get_pid(uid);
126                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
127                 } else {
128                         g_wait_timer = ecore_timer_add(0, __wait_synthesis, NULL);
129                 }
130
131                 free(sdata.lang);
132                 free(sdata.text);
133         }
134
135         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS  END");
136         SLOG(LOG_DEBUG, get_tag(), "  ");
137
138         return 0;
139 }
140
141 /*
142 * TTS Server Callback Functions 
143 */
144 int __synthesis_result_callback(ttsp_result_event_e event, const void* data, unsigned int data_size, 
145                                 ttsp_audio_type_e audio_type, int rate, void *user_data)
146 {
147         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK START");
148
149         int uid = g_utt.uid;
150         int uttid = g_utt.uttid;
151
152         /* Synthesis is success */
153         if (TTSP_RESULT_EVENT_START == event || TTSP_RESULT_EVENT_CONTINUE == event || TTSP_RESULT_EVENT_FINISH == event) {
154                 
155                 if (TTSP_RESULT_EVENT_START == event)           SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_START");
156                 if (TTSP_RESULT_EVENT_CONTINUE == event)        SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_CONTINUE");
157                 if (TTSP_RESULT_EVENT_FINISH == event)          SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_FINISH");
158
159                 if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
160                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
161                         SECURE_SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%d), uttid(%d)", uid, uttid);
162                         SLOG(LOG_DEBUG, get_tag(), "=====");
163                         SLOG(LOG_DEBUG, get_tag(), "  ");
164                         return 0;
165                 }
166
167                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[SERVER] Result Info : uid(%d), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)", 
168                         uid, uttid, data, data_size, audio_type, rate);
169
170                 if (rate <= 0 || audio_type < 0 || audio_type > TTSP_AUDIO_TYPE_MAX) {
171                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
172                         SECURE_SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] audio data is invalid");
173                         SLOG(LOG_DEBUG, get_tag(), "=====");
174                         SLOG(LOG_DEBUG, get_tag(), "  ");
175                         return 0;
176                 }
177
178                 /* add wav data */
179                 sound_data_s temp_data;
180                 temp_data.data = NULL;
181                 temp_data.rate = 0;
182                 temp_data.data_size = 0;
183
184                 if (0 < data_size) {
185                         temp_data.data = (char*)calloc(data_size, sizeof(char));
186                         memcpy(temp_data.data, data, data_size);
187                 } else {
188                         SLOG(LOG_ERROR, get_tag(), "Sound data is NULL");
189                 }
190
191                 temp_data.data_size = data_size;
192                 temp_data.utt_id = uttid;
193                 temp_data.event = event;
194                 temp_data.audio_type = audio_type;
195                 temp_data.rate = rate;
196                 
197                 if (0 != ttsd_data_add_sound_data(uid, temp_data)) {
198                         SECURE_SLOG(LOG_ERROR, get_tag(), "[SERVER ERROR] Fail to add sound data : uid(%d)", uid);
199                 }
200
201                 if (event == TTSP_RESULT_EVENT_FINISH) {
202                         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
203                 }
204
205                 if (0 != ttsd_player_play(uid)) {
206                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to play sound : uid(%d)", uid);
207
208                         /* Change ready state */
209                         ttsd_server_stop(uid);
210
211                         int tmp_pid;
212                         tmp_pid = ttsd_data_get_pid(uid);
213                         ttsdc_send_set_state_message(tmp_pid, uid, APP_STATE_READY);
214                 }
215         } else {
216                 SLOG(LOG_DEBUG, get_tag(), "[SERVER] Event : TTSP_RESULT_EVENT_ERROR");
217                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
218         } 
219
220
221         SLOG(LOG_DEBUG, get_tag(), "===== SYNTHESIS RESULT CALLBACK END");
222         SLOG(LOG_DEBUG, get_tag(), "  ");
223
224         return 0;
225 }
226
227 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
228 {
229         /* clear client data */
230         ttsd_data_clear_data(uid);                      
231         ttsd_data_set_client_state(uid, APP_STATE_READY);
232
233         /* send message */
234         if ( 0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
235                 /* remove client */
236                 ttsd_data_delete_client(uid);
237         } 
238
239         return true;
240 }
241
242 void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param)
243 {
244         switch(type) {
245         case TTS_CONFIG_TYPE_ENGINE:
246         {
247                 if (NULL == str_param) {
248                         SLOG(LOG_ERROR, get_tag(), "[Server] engine id from config is NULL");
249                         return;
250                 }
251
252                 int ret = 0;
253                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
254                         SLOG(LOG_DEBUG, get_tag(), "[Server Setting] new engine is the same as current engine");
255                         ret = ttsd_engine_agent_unload_current_engine();
256                         if (0 != ret) {
257                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
258                         }
259
260                         ret = ttsd_engine_agent_load_current_engine();
261                         if (0 != ret) {
262                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
263                         }
264                         return;
265                 }
266
267                 /* stop all player */ 
268                 ttsd_player_all_stop();
269
270                 /* send interrupt message to  all clients */
271                 ttsd_data_foreach_clients(__get_client_cb, NULL);
272
273                 ttsd_engine_cancel_synthesis();
274
275                 /* set engine */
276                 ret = ttsd_engine_agent_set_default_engine(str_param);
277                 if (0 != ret) {
278                         SLOG(LOG_WARN, get_tag(), "[Server Setting WARNING] Fail to set current engine : result(%d)", ret);
279                 }
280
281                 break;
282         }
283
284         case TTS_CONFIG_TYPE_VOICE:
285         {
286                 if (NULL == str_param) {
287                         SLOG(LOG_ERROR, get_tag(), "[Server] language from config is NULL");
288                         return;
289                 }
290
291                 char* out_lang = NULL;
292                 int out_type;
293                 int ret = -1;
294
295                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
296                         SECURE_SLOG(LOG_ERROR, get_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
297                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
298                         if (0 != ret)
299                                 SECURE_SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
300                 } else {
301                         /* Current language is not available */
302                         SECURE_SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
303                 }
304                 if (NULL != out_lang)   free(out_lang);
305                 break;
306         }
307
308         case TTS_CONFIG_TYPE_SPEED:
309         {
310                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
311                         /* set default speed */
312                         int ret = 0;
313                         ret = ttsd_engine_agent_set_default_speed(int_param);
314                         if (0 != ret) {
315                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
316                         }       
317                 }
318                 break;
319         }
320
321         case TTS_CONFIG_TYPE_PITCH:
322         {
323                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
324                         /* set default speed */
325                         int ret = 0;
326                         ret = ttsd_engine_agent_set_default_pitch(int_param);
327                         if (0 != ret) {
328                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
329                         }       
330                 }
331                 break;
332         }
333
334         default:
335                 break;
336         }
337         
338         return;
339 }
340
341 bool __terminate_client(int pid, int uid, app_state_e state, void* user_data)
342 {
343         SLOG(LOG_DEBUG, get_tag(), "=== Start to terminate client [%d] ===", uid);
344         ttsd_server_finalize(uid);
345         return true;
346 }
347
348 Eina_Bool ttsd_terminate_daemon(void *data) 
349 {
350         ttsd_data_foreach_clients(__terminate_client, NULL);
351         return EINA_FALSE;
352 }
353
354 void __screen_reader_changed_cb(bool value)
355 {
356         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && false == value) {
357                 SLOG(LOG_DEBUG, get_tag(), "[Server] Screen reader is OFF. Start to terminate tts daemon");
358                 ecore_timer_add(1, ttsd_terminate_daemon, NULL);
359         } else {
360                 SLOG(LOG_DEBUG, get_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
361         }
362         return;
363 }
364
365 /*
366 * Server APIs
367 */
368
369 int ttsd_initialize()
370 {
371         if (ttsd_config_initialize(__config_changed_cb)) {
372                 SLOG(LOG_ERROR, get_tag(), "[Server WARNING] Fail to initialize config.");
373         }
374
375         /* player init */
376         if (ttsd_player_init()) {
377                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to initialize player init.");
378                 return TTSD_ERROR_OPERATION_FAILED;
379         }
380
381         /* Engine Agent initialize */
382         if (0 != ttsd_engine_agent_init(__synthesis_result_callback)) {
383                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to engine agent initialize.");
384                 return TTSD_ERROR_OPERATION_FAILED;
385         }
386
387         /* set current engine */
388         if (0 != ttsd_engine_agent_initialize_current_engine()) {
389                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!" );
390                 g_is_engine = false;
391         } else 
392                 g_is_engine = true;
393         
394         __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
395
396         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
397                 ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
398         }
399
400         ttsd_file_clean_up();
401
402         return TTSD_ERROR_NONE;
403 }
404
405 int ttsd_finalize()
406 {
407         ttsd_config_finalize();
408         
409         ttsd_player_release();
410
411         ttsd_engine_agent_release();
412
413         return TTSD_ERROR_NONE;
414 }
415
416 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
417 {
418         char appid[128] = {0, };
419         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
420                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get app id");
421         }
422
423         if (0 < strlen(appid)) {
424                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%d] is running app - %s", pid, appid);
425         } else {
426                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%d] is daemon or no_running app", pid);
427
428                 int result = 1;
429                 result = ttsdc_send_hello(pid, uid);
430
431                 if (0 == result) {
432                         SECURE_SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid); 
433                         ttsd_server_finalize(uid);
434                 } else if (-1 == result) {
435                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error"); 
436                 }
437         }
438         return true;
439 }
440
441
442 Eina_Bool ttsd_cleanup_client(void *data)
443 {
444         SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
445         ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
446         SLOG(LOG_DEBUG, get_tag(), "=====");
447         SLOG(LOG_DEBUG, get_tag(), "  ");
448
449         return EINA_TRUE;
450 }
451
452 /*
453 * TTS Server Functions for Client
454 */
455
456 int ttsd_server_initialize(int pid, int uid)
457 {
458         if (false == g_is_engine) {
459                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
460                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!");
461                         g_is_engine = false;
462
463                         return TTSD_ERROR_ENGINE_NOT_FOUND;
464                 } else {
465                         g_is_engine = true;
466                 }
467         }
468
469         if (-1 != ttsd_data_is_client(uid)) {
470                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Uid has already been registered");
471                 return TTSD_ERROR_NONE;
472         }
473
474         if (0 == ttsd_data_get_client_count()) {
475                 if (0 != ttsd_engine_agent_load_current_engine()) {
476                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine");
477                         return TTSD_ERROR_OPERATION_FAILED;
478                 }
479         }
480
481         if (0 == ttsd_data_get_same_pid_client_count(pid)) {
482                 if (0 != ttsd_file_msg_open_connection(pid)) {
483                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to open file message connection");
484                         return TTSD_ERROR_OPERATION_FAILED;
485                 }
486         }
487
488         if (0 != ttsd_data_new_client(pid, uid)) {
489                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info");
490                 return TTSD_ERROR_OPERATION_FAILED;
491         }
492
493         if (0 != ttsd_player_create_instance(uid)) {
494                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player");
495                 return TTSD_ERROR_OPERATION_FAILED;
496         }
497
498         return TTSD_ERROR_NONE;
499 }
500
501 static Eina_Bool __quit_ecore_loop(void *data)
502 {
503         ecore_main_loop_quit();
504         return EINA_FALSE;
505 }
506
507 void __used_voice_cb(const char* lang, int type)
508 {
509         SLOG(LOG_DEBUG, get_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
510         if (0 != ttsd_engine_unload_voice(lang, type)) {
511                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to unload voice");
512         }
513 }
514
515 int ttsd_server_finalize(int uid)
516 {
517         app_state_e state;
518         if (0 > ttsd_data_get_client_state(uid, &state)) {
519                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
520         }
521
522         ttsd_server_stop(uid);
523         
524         ttsd_player_destroy_instance(uid);
525
526         /* Need to unload voice when used voice is unregistered */
527         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
528                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set used voice");
529                 return TTSD_ERROR_OPERATION_FAILED;
530         }
531
532         int pid = ttsd_data_get_pid(uid);
533
534         ttsd_data_delete_client(uid);
535
536         if (0 == ttsd_data_get_same_pid_client_count(pid)) {
537                 ttsd_file_msg_close_connection(pid);
538         }
539
540         /* unload engine, if ref count of client is 0 */
541         if (0 == ttsd_data_get_client_count()) {
542                 SLOG(LOG_DEBUG, get_tag(), "[Server] Quit main loop");
543                 ecore_timer_add(0, __quit_ecore_loop, NULL);
544         }
545
546         return TTSD_ERROR_NONE;
547 }
548
549 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
550 {
551         app_state_e state;
552         if (0 > ttsd_data_get_client_state(uid, &state)) {
553                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
554                 return TTSD_ERROR_INVALID_PARAMETER;
555         }
556
557         /* check valid voice */
558         char* temp_lang = NULL;
559         int temp_type;
560         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
561                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice");
562                 if (NULL != temp_lang)  free(temp_lang);
563                 return TTSD_ERROR_INVALID_VOICE;
564         }
565
566         if (NULL == temp_lang) {
567                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
568                 return TTSD_ERROR_INVALID_VOICE;
569         }
570         
571         speak_data_s data;
572
573         data.lang = strdup(lang);
574         data.vctype = voice_type;
575
576         data.speed = speed;
577         data.utt_id = utt_id;
578
579         data.text = strdup(text);
580
581         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
582         if (0 != ttsd_data_add_speak_data(uid, data)) {
583                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add speak data");
584                 if (NULL != temp_lang)  free(temp_lang);
585                 return TTSD_ERROR_OPERATION_FAILED;
586         }
587
588         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
589                 /* Request load voice */
590                 SLOG(LOG_DEBUG, get_tag(), "[Server] Request to load voice");
591                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
592                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load voice");
593                 }
594         }
595
596         if (NULL != temp_lang)  free(temp_lang);
597
598         if (APP_STATE_PLAYING == state) {
599                 /* check if engine use network */
600                 if (ttsd_engine_agent_need_network()) {
601                         if (false == ttsd_network_is_connected()) {
602                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
603                                 return TTSD_ERROR_OPERATION_FAILED;
604                         }
605                 }
606
607                 /* Check whether tts-engine is running or not */
608                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
609                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
610                 } else {
611                         __synthesis(uid);
612                 }
613         }
614
615         return TTSD_ERROR_NONE;
616 }
617
618 Eina_Bool __send_interrupt_client(void *data)
619 {
620         int uid = (int)data;
621         
622         int pid = ttsd_data_get_pid(uid);
623
624         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
625                 /* send message to client about changing state */
626                 ttsdc_send_set_state_message (pid, uid, APP_STATE_READY);
627         } else {
628                 ttsdc_send_set_state_message (pid, uid, APP_STATE_PAUSED);
629         }
630
631         return EINA_FALSE;
632 }
633
634 int ttsd_server_play(int uid)
635 {
636         app_state_e state;
637         if (0 > ttsd_data_get_client_state(uid, &state)) {
638                 SECURE_SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
639                 return TTSD_ERROR_INVALID_PARAMETER;
640         }
641         
642         if (APP_STATE_PLAYING == state) {
643                 SECURE_SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
644                 return TTSD_ERROR_NONE;
645         }
646
647         /* check if engine use network */
648         if (ttsd_engine_agent_need_network()) {
649                 if (false == ttsd_network_is_connected()) {
650                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
651                         return TTSD_ERROR_OUT_OF_NETWORK;
652                 }
653         }
654
655         int current_uid = ttsd_data_get_current_playing();
656
657         if (uid != current_uid && -1 != current_uid) {
658                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
659                         /* Send interrupt message */
660                         SECURE_SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
661
662                         /* pause player */
663                         if (0 != ttsd_server_stop(current_uid)) {
664                                 SECURE_SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
665                         } 
666                         
667                         ecore_timer_add(0, __send_interrupt_client, (void*)current_uid);
668                 } else {
669                         /* Default mode policy of interrupt is "Pause" */
670
671                         /* Send interrupt message */
672                         SECURE_SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
673
674                         /* pause player */
675                         if (0 != ttsd_player_pause(current_uid)) {
676                                 SECURE_SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
677                         }
678
679                         /* change state */
680                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
681
682                         ecore_timer_add(0, __send_interrupt_client, (void*)current_uid);
683                 }
684         }
685
686         /* Change current play */
687         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
688                 SECURE_SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
689                 return TTSD_ERROR_OPERATION_FAILED;
690         }
691
692         if (APP_STATE_PAUSED == state) {
693                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
694
695                 /* Resume player */
696                 if (0 != ttsd_player_resume(uid)) {
697                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
698                 }
699         }
700
701         /* Check whether tts-engine is running or not */
702         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
703                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
704         } else {
705                 __synthesis(uid);
706         }
707
708         return TTSD_ERROR_NONE;
709 }
710
711
712 int ttsd_server_stop(int uid)
713 {
714         app_state_e state;
715         if (0 > ttsd_data_get_client_state(uid, &state)) {
716                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
717                 return TTSD_ERROR_INVALID_PARAMETER;
718         }
719
720         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
721                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
722                         SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running");
723
724                         int ret = 0;
725                         ret = ttsd_engine_cancel_synthesis();
726                         if (0 != ret)
727                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
728                 }
729
730                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
731
732                 if (0 != ttsd_player_stop(uid))
733                         SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
734
735                 ttsd_data_set_client_state(uid, APP_STATE_READY);
736         }
737
738         /* Reset all data */
739         ttsd_data_clear_data(uid);
740
741         return TTSD_ERROR_NONE;
742 }
743
744 int ttsd_server_pause(int uid, int* utt_id)
745 {
746         app_state_e state;
747         if (0 > ttsd_data_get_client_state(uid, &state)) {
748                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
749                 return TTSD_ERROR_INVALID_PARAMETER;
750         }
751
752         if (APP_STATE_PLAYING != state) {
753                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play'");
754                 return TTSD_ERROR_INVALID_STATE;
755         }
756
757         int ret = 0;
758         ret = ttsd_player_pause(uid);
759         if (0 != ret) {
760                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
761                 return TTSD_ERROR_OPERATION_FAILED;
762         }
763
764         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
765
766         return TTSD_ERROR_NONE;
767 }
768
769 int ttsd_server_get_support_voices(int uid, GList** voice_list)
770 {
771         app_state_e state;
772         if (0 > ttsd_data_get_client_state(uid, &state)) {
773                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
774                 return TTSD_ERROR_INVALID_PARAMETER;
775         }
776
777         /* get voice list*/
778         if (0 != ttsd_engine_get_voice_list(voice_list)) {
779                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
780                 return TTSD_ERROR_OPERATION_FAILED;
781         }
782
783         SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices");
784
785         return TTSD_ERROR_NONE;
786 }
787
788 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
789 {
790         app_state_e state;
791         if (0 > ttsd_data_get_client_state(uid, &state)) {
792                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
793                 return TTSD_ERROR_INVALID_PARAMETER;
794         }               
795
796         /* get current voice */
797         int ret = ttsd_engine_get_default_voice(language, voice_type);
798         if (0 != ret) {
799                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
800                 return ret;
801         }
802
803         SECURE_SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type); 
804
805         return TTSD_ERROR_NONE;
806 }