7b452feaf69102b62936f15e58aaa2751b1b8828
[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                 SLOG(LOG_DEBUG, get_tag(), "ID : uid (%d), uttid(%d) ", g_utt.uid, g_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, 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                         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                 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                         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                         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                         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                                 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                         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         return TTSD_ERROR_NONE;
401 }
402
403 int ttsd_finalize()
404 {
405         ttsd_config_finalize();
406
407         ttsd_player_release();
408
409         ttsd_engine_agent_release();
410
411         return TTSD_ERROR_NONE;
412 }
413
414 bool __get_client_for_clean_up(int pid, int uid, app_state_e state, void* user_data)
415 {
416         char appid[128] = {0, };
417         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
418                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to get app id");
419         }
420
421         if (0 < strlen(appid)) {
422                 SLOG(LOG_DEBUG, get_tag(), "[%d] is running app - %s", pid, appid);
423         } else {
424                 SLOG(LOG_DEBUG, get_tag(), "[%d] is daemon or no_running app", pid);
425
426                 int result = 1;
427                 result = ttsdc_send_hello(pid, uid);
428
429                 if (0 == result) {
430                         SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) should be removed.", uid);
431                         ttsd_server_finalize(uid);
432                 } else if (-1 == result) {
433                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Hello result has error");
434                 }
435         }
436         return true;
437 }
438
439
440 Eina_Bool ttsd_cleanup_client(void *data)
441 {
442         SLOG(LOG_DEBUG, get_tag(), "===== CLEAN UP CLIENT START");
443         ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
444         SLOG(LOG_DEBUG, get_tag(), "=====");
445         SLOG(LOG_DEBUG, get_tag(), "  ");
446
447         return EINA_TRUE;
448 }
449
450 /*
451 * TTS Server Functions for Client
452 */
453
454 int ttsd_server_initialize(int pid, int uid)
455 {
456         if (false == g_is_engine) {
457                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
458                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] No Engine !!!");
459                         g_is_engine = false;
460
461                         return TTSD_ERROR_ENGINE_NOT_FOUND;
462                 } else {
463                         g_is_engine = true;
464                 }
465         }
466
467         if (-1 != ttsd_data_is_client(uid)) {
468                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Uid has already been registered");
469                 return TTSD_ERROR_NONE;
470         }
471
472         if (0 == ttsd_data_get_client_count()) {
473                 if (0 != ttsd_engine_agent_load_current_engine()) {
474                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load current engine");
475                         return TTSD_ERROR_OPERATION_FAILED;
476                 }
477         }
478
479         if (0 != ttsd_data_new_client(pid, uid)) {
480                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add client info");
481                 return TTSD_ERROR_OPERATION_FAILED;
482         }
483
484         if (0 != ttsd_player_create_instance(uid)) {
485                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to create player");
486                 return TTSD_ERROR_OPERATION_FAILED;
487         }
488
489         return TTSD_ERROR_NONE;
490 }
491
492 static Eina_Bool __quit_ecore_loop(void *data)
493 {
494         ecore_main_loop_quit();
495         return EINA_FALSE;
496 }
497
498 void __used_voice_cb(const char* lang, int type)
499 {
500         SLOG(LOG_DEBUG, get_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
501         if (0 != ttsd_engine_unload_voice(lang, type)) {
502                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to unload voice");
503         }
504 }
505
506 int ttsd_server_finalize(int uid)
507 {
508         app_state_e state;
509         if (0 > ttsd_data_get_client_state(uid, &state)) {
510                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
511         }
512
513         ttsd_server_stop(uid);
514
515         ttsd_player_destroy_instance(uid);
516
517         /* Need to unload voice when used voice is unregistered */
518         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
519                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set used voice");
520                 return TTSD_ERROR_OPERATION_FAILED;
521         }
522
523         ttsd_data_delete_client(uid);
524
525         /* unload engine, if ref count of client is 0 */
526         if (0 == ttsd_data_get_client_count()) {
527                 SLOG(LOG_DEBUG, get_tag(), "[Server] Quit main loop");
528                 ecore_timer_add(0, __quit_ecore_loop, NULL);
529         }
530
531         return TTSD_ERROR_NONE;
532 }
533
534 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
535 {
536         app_state_e state;
537         if (0 > ttsd_data_get_client_state(uid, &state)) {
538                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_add_queue : uid is not valid");
539                 return TTSD_ERROR_INVALID_PARAMETER;
540         }
541
542         /* check valid voice */
543         char* temp_lang = NULL;
544         int temp_type;
545         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
546                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice");
547                 if (NULL != temp_lang)  free(temp_lang);
548                 return TTSD_ERROR_INVALID_VOICE;
549         }
550
551         if (NULL == temp_lang) {
552                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
553                 return TTSD_ERROR_INVALID_VOICE;
554         }
555
556         speak_data_s data;
557
558         data.lang = strdup(lang);
559         data.vctype = voice_type;
560
561         data.speed = speed;
562         data.utt_id = utt_id;
563
564         data.text = strdup(text);
565
566         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
567         if (0 != ttsd_data_add_speak_data(uid, data)) {
568                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to add speak data");
569                 if (NULL != temp_lang)  free(temp_lang);
570                 return TTSD_ERROR_OPERATION_FAILED;
571         }
572
573         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
574                 /* Request load voice */
575                 SLOG(LOG_DEBUG, get_tag(), "[Server] Request to load voice");
576                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
577                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to load voice");
578                 }
579         }
580
581         if (NULL != temp_lang)  free(temp_lang);
582
583         if (APP_STATE_PLAYING == state) {
584                 /* check if engine use network */
585                 if (ttsd_engine_agent_need_network()) {
586                         if (false == ttsd_network_is_connected()) {
587                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
588                                 return TTSD_ERROR_OPERATION_FAILED;
589                         }
590                 }
591
592                 /* Check whether tts-engine is running or not */
593                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
594                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
595                 } else {
596                         __synthesis(uid);
597                 }
598         }
599
600         return TTSD_ERROR_NONE;
601 }
602
603 Eina_Bool __send_interrupt_client(void *data)
604 {
605         int uid = (int)data;
606
607         int pid = ttsd_data_get_pid(uid);
608
609         if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
610                 /* send message to client about changing state */
611                 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
612         } else {
613                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
614         }
615
616         return EINA_FALSE;
617 }
618
619 int ttsd_server_play(int uid)
620 {
621         app_state_e state;
622         if (0 > ttsd_data_get_client_state(uid, &state)) {
623                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid(%d) is NOT valid  ", uid);
624                 return TTSD_ERROR_INVALID_PARAMETER;
625         }
626
627         if (APP_STATE_PLAYING == state) {
628                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state(%d) is 'play' ", uid);
629                 return TTSD_ERROR_NONE;
630         }
631
632         /* check if engine use network */
633         if (ttsd_engine_agent_need_network()) {
634                 if (false == ttsd_network_is_connected()) {
635                         SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
636                         return TTSD_ERROR_OUT_OF_NETWORK;
637                 }
638         }
639
640         int current_uid = ttsd_data_get_current_playing();
641
642         if (uid != current_uid && -1 != current_uid) {
643                 if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
644                         /* Send interrupt message */
645                         SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Stop' state ", current_uid);
646
647                         /* pause player */
648                         if (0 != ttsd_server_stop(current_uid)) {
649                                 SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to stop : uid (%d)", current_uid);
650                         }
651
652                         ecore_timer_add(0, __send_interrupt_client, (void*)current_uid);
653                 } else {
654                         /* Default mode policy of interrupt is "Pause" */
655
656                         /* Send interrupt message */
657                         SLOG(LOG_DEBUG, get_tag(), "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
658
659                         /* pause player */
660                         if (0 != ttsd_player_pause(current_uid)) {
661                                 SLOG(LOG_WARN, get_tag(), "[Server ERROR] Fail to ttsd_player_pause() : uid (%d)", current_uid);
662                         }
663
664                         /* change state */
665                         ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
666
667                         ecore_timer_add(0, __send_interrupt_client, (void*)current_uid);
668                 }
669         }
670
671         /* Change current play */
672         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
673                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to set state : uid(%d)", uid);
674                 return TTSD_ERROR_OPERATION_FAILED;
675         }
676
677         if (APP_STATE_PAUSED == state) {
678                 SLOG(LOG_DEBUG, get_tag(), "[Server] uid(%d) is 'Pause' state : resume player", uid);
679
680                 /* Resume player */
681                 if (0 != ttsd_player_resume(uid)) {
682                         SLOG(LOG_WARN, get_tag(), "[Server WARNING] Fail to ttsd_player_resume()");
683                 }
684         }
685
686         /* Check whether tts-engine is running or not */
687         if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
688                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Engine has already been running.");
689         } else {
690                 __synthesis(uid);
691         }
692
693         return TTSD_ERROR_NONE;
694 }
695
696
697 int ttsd_server_stop(int uid)
698 {
699         app_state_e state;
700         if (0 > ttsd_data_get_client_state(uid, &state)) {
701                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
702                 return TTSD_ERROR_INVALID_PARAMETER;
703         }
704
705         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
706                 if (TTSD_SYNTHESIS_CONTROL_DOING == __server_get_synth_control()) {
707                         SLOG(LOG_DEBUG, get_tag(), "[Server] TTS-engine is running");
708
709                         int ret = 0;
710                         ret = ttsd_engine_cancel_synthesis();
711                         if (0 != ret)
712                                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
713                 }
714
715                 __server_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
716
717                 if (0 != ttsd_player_stop(uid))
718                         SLOG(LOG_WARN, get_tag(), "[Server] Fail to ttsd_player_stop()");
719
720                 ttsd_data_set_client_state(uid, APP_STATE_READY);
721         }
722
723         /* Reset all data */
724         ttsd_data_clear_data(uid);
725
726         return TTSD_ERROR_NONE;
727 }
728
729 int ttsd_server_pause(int uid, int* utt_id)
730 {
731         app_state_e state;
732         if (0 > ttsd_data_get_client_state(uid, &state)) {
733                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
734                 return TTSD_ERROR_INVALID_PARAMETER;
735         }
736
737         if (APP_STATE_PLAYING != state) {
738                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] Current state is not 'play'");
739                 return TTSD_ERROR_INVALID_STATE;
740         }
741
742         int ret = 0;
743         ret = ttsd_player_pause(uid);
744         if (0 != ret) {
745                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
746                 return TTSD_ERROR_OPERATION_FAILED;
747         }
748
749         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
750
751         return TTSD_ERROR_NONE;
752 }
753
754 int ttsd_server_get_support_voices(int uid, GList** voice_list)
755 {
756         app_state_e state;
757         if (0 > ttsd_data_get_client_state(uid, &state)) {
758                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] uid is not valid");
759                 return TTSD_ERROR_INVALID_PARAMETER;
760         }
761
762         /* get voice list*/
763         if (0 != ttsd_engine_get_voice_list(voice_list)) {
764                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
765                 return TTSD_ERROR_OPERATION_FAILED;
766         }
767
768         SLOG(LOG_DEBUG, get_tag(), "[Server SUCCESS] Get supported voices");
769
770         return TTSD_ERROR_NONE;
771 }
772
773 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
774 {
775         app_state_e state;
776         if (0 > ttsd_data_get_client_state(uid, &state)) {
777                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
778                 return TTSD_ERROR_INVALID_PARAMETER;
779         }
780
781         /* get current voice */
782         int ret = ttsd_engine_get_default_voice(language, voice_type);
783         if (0 != ret) {
784                 SLOG(LOG_ERROR, get_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices()");
785                 return ret;
786         }
787
788         SLOG(LOG_DEBUG, get_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
789
790         return TTSD_ERROR_NONE;
791 }