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