Fix PLM issue(P130121-3243) and Add Daemon termination
[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 static Eina_Bool __quit_ecore_loop(void *data)
499 {
500         ecore_main_loop_quit();
501         SLOG(LOG_DEBUG, TAG_TTSD, "[Server] quit ecore main loop");
502         return EINA_FALSE;
503 }
504
505 int ttsd_server_finalize(int uid)
506 {
507         app_state_e state;
508         if (0 > ttsd_data_get_client_state(uid, &state)) {
509                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] ttsd_server_finalize : uid is not valid  ");
510                 return TTSD_ERROR_INVALID_PARAMETER;
511         }
512
513         ttsd_server_stop(uid);
514         
515         ttsd_player_destroy_instance(uid);
516
517         ttsd_data_delete_client(uid);
518
519         /* unload engine, if ref count of client is 0 */
520         if (0 == ttsd_data_get_client_count()) {
521                 if (0 != ttsd_engine_agent_release()) {
522                         SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] fail to release engine agent");
523                 } else {
524                         SLOG(LOG_DEBUG, TAG_TTSD, "[Server SUCCESS] release engine agent");
525                 }
526
527                 ecore_timer_add(0, __quit_ecore_loop, NULL);
528         }
529
530         return TTSD_ERROR_NONE;
531 }
532
533 int ttsd_server_add_queue(int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id)
534 {
535         app_state_e state;
536         if (0 > ttsd_data_get_client_state(uid, &state)) {
537                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] ttsd_server_add_queue : uid is not valid  ");
538                 return TTSD_ERROR_INVALID_PARAMETER;
539         }
540
541         /* check valid voice */
542         char* temp_lang = NULL;
543         ttsp_voice_type_e temp_type;
544         if (true != ttsd_engine_select_valid_voice((const char*)lang, (const ttsp_voice_type_e)voice_type, &temp_lang, &temp_type)) {
545                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Fail to select valid voice ");
546                 return TTSD_ERROR_INVALID_VOICE;
547         } else {                
548                 if (NULL == temp_lang)
549                         free(temp_lang);
550         }
551         
552         speak_data_s data;
553
554         data.lang = strdup(lang);
555         data.vctype = (ttsp_voice_type_e)voice_type;
556
557         data.speed = (ttsp_speed_e)speed;
558         data.utt_id = utt_id;
559                 
560         data.text = strdup(text);
561
562         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
563         if (0 != ttsd_data_add_speak_data(uid, data)) {
564                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] ttsd_server_add_queue : Current state of uid is not 'ready' ");
565                 return TTSD_ERROR_OPERATION_FAILED;
566         }
567
568         if (APP_STATE_PLAYING == state) {
569                 /* check if engine use network */
570                 if (ttsd_engine_agent_need_network()) {
571                         if (false == ttsd_network_is_connected()) {
572                                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Disconnect network. Current engine needs network.");
573                                 return TTSD_ERROR_OPERATION_FAILED;
574                         }
575                 }
576
577                 /* mode 2 for add text */
578                 if (0 != __server_start_synthesis(uid, 2)) {
579                         SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] fail to schedule synthesis : uid(%d)", uid);
580                         return TTSD_ERROR_OPERATION_FAILED;
581                 }
582         }
583
584         return TTSD_ERROR_NONE;
585 }
586
587 Eina_Bool __send_interrupt_client(void *data)
588 {
589         int* uid = (int*)data;
590         
591         if (NULL != uid) {
592                 int pid = ttsd_data_get_pid(*uid);
593                 /* send message to client about changing state */
594                 ttsdc_send_set_state_message (pid, *uid, APP_STATE_PAUSED);
595                 free(uid);
596         }
597         return EINA_FALSE;
598 }
599
600 int ttsd_server_play(int uid)
601 {
602         app_state_e state;
603         if (0 > ttsd_data_get_client_state(uid, &state)) {
604                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] uid(%d) is NOT valid  ", uid);
605                 return TTSD_ERROR_INVALID_PARAMETER;
606         }
607         
608         if (APP_STATE_PLAYING == state) {
609                 SLOG(LOG_WARN, TAG_TTSD, "[Server WARNING] Current state(%d) is 'play' ", uid);
610                 return TTSD_ERROR_NONE;
611         }
612
613         /* check if engine use network */
614         if (ttsd_engine_agent_need_network()) {
615                 if (false == ttsd_network_is_connected()) {
616                         SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
617                         return TTSD_ERROR_OUT_OF_NETWORK;
618                 }
619         }
620
621         int current_uid = ttsd_data_get_current_playing();
622
623         if (uid != current_uid && -1 != current_uid) {
624                 /* Send interrupt message */
625                 SLOG(LOG_DEBUG, TAG_TTSD, "[Server] Old uid(%d) will be interrupted into 'Pause' state ", current_uid);
626
627                 /* pause player */
628                 if (0 != ttsd_player_pause(current_uid)) {
629                         SLOG(LOG_WARN, TAG_TTSD, "[Server ERROR] fail to ttsd_player_pause() : uid (%d)", current_uid);
630                 } 
631
632                 /* change state */
633                 ttsd_data_set_client_state(current_uid, APP_STATE_PAUSED);
634
635                 int* temp_uid = (int*)malloc(sizeof(int));
636                 *temp_uid = current_uid;
637                 ecore_timer_add(0, __send_interrupt_client, temp_uid);
638         }
639         
640         /* Change current play */
641         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
642                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Fail to set state : uid(%d)", uid);
643                 return TTSD_ERROR_OPERATION_FAILED;
644         }
645
646         if (0 != __server_play_internal(uid, state)) {
647                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Fail to start synthesis : uid(%d)", uid);
648                 return TTSD_ERROR_OPERATION_FAILED;
649         }
650
651         ecore_timer_add(0, __start_next_synthesis, NULL);
652
653         return TTSD_ERROR_NONE;
654 }
655
656
657 int ttsd_server_stop(int uid)
658 {
659         app_state_e state;
660         if (0 > ttsd_data_get_client_state(uid, &state)) {
661                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] uid is not valid  ");
662                 return TTSD_ERROR_INVALID_PARAMETER;
663         }
664
665         /* Reset all data */
666         ttsd_data_clear_data(uid);
667
668         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
669                 ttsd_data_set_client_state(uid, APP_STATE_READY);
670
671                 if (0 != ttsd_player_stop(uid)) 
672                         SLOG(LOG_WARN, TAG_TTSD, "[Server] Fail to ttsd_player_stop()");
673
674                 if (true == __server_get_is_synthesizing()) {
675                         SLOG(LOG_DEBUG, TAG_TTSD, "[Server] TTS-engine is running ");
676
677                         int ret = 0;
678                         ret = ttsd_engine_cancel_synthesis();
679                         if (0 != ret)
680                                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
681
682                         __server_set_is_synthesizing(false);
683                 } 
684         } else {
685                 SLOG(LOG_WARN, TAG_TTSD, "[Server WARNING] Current state is 'ready' ");
686         }
687
688         return TTSD_ERROR_NONE;
689 }
690
691 int ttsd_server_pause(int uid, int* utt_id)
692 {
693         app_state_e state;
694         if (0 > ttsd_data_get_client_state(uid, &state)) {
695                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] ttsd_server_pause : uid is not valid  ");
696                 return TTSD_ERROR_INVALID_PARAMETER;
697         }
698
699         if (APP_STATE_PLAYING != state) {
700                 SLOG(LOG_WARN, TAG_TTSD, "[Server WARNING] Current state is not 'play' ");
701                 return TTSD_ERROR_INVALID_STATE;
702         }
703
704         int ret = 0;
705         ret = ttsd_player_pause(uid);
706         if (0 != ret) {
707                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] fail player_pause() : ret(%d)", ret);
708                 return TTSD_ERROR_OPERATION_FAILED;
709         }
710
711         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
712
713         return TTSD_ERROR_NONE;
714 }
715
716 int ttsd_server_get_support_voices(int uid, GList** voice_list)
717 {
718         app_state_e state;
719         if (0 > ttsd_data_get_client_state(uid, &state)) {
720                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] uid is not valid  ");
721                 return TTSD_ERROR_INVALID_PARAMETER;
722         }
723
724         /* get voice list*/
725         if (0 != ttsd_engine_get_voice_list(voice_list)) {
726                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] Fail ttsd_server_get_support_voices() ");
727                 return TTSD_ERROR_OPERATION_FAILED;
728         }
729
730         SLOG(LOG_DEBUG, TAG_TTSD, "[Server SUCCESS] Get supported voices ");
731
732         return TTSD_ERROR_NONE;
733 }
734
735 int ttsd_server_get_current_voice(int uid, char** language, int* voice_type)
736 {
737         app_state_e state;
738         if (0 > ttsd_data_get_client_state(uid, &state)) {
739                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] ttsd_server_get_current_voice : uid is not valid  ");
740                 return TTSD_ERROR_INVALID_PARAMETER;
741         }               
742
743         /* get current voice */
744         int ret = ttsd_engine_get_default_voice(language, (ttsp_voice_type_e*)voice_type);
745         if (0 != ret) {
746                 SLOG(LOG_ERROR, TAG_TTSD, "[Server ERROR] fail ttsd_server_get_support_voices() ");
747                 return ret;
748         }
749
750         SLOG(LOG_DEBUG, TAG_TTSD, "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type); 
751
752         return TTSD_ERROR_NONE;
753 }
754
755
756 /*
757 * TTS Server Functions for Setting                                                                                                                *
758 */
759
760 int ttsd_server_setting_initialize(int uid)
761 {
762         if (false == g_is_engine) {
763                 if (0 != ttsd_engine_agent_initialize_current_engine()) {
764                         SLOG(LOG_WARN, TAG_TTSD, "[Server Setting WARNING] No Engine !!! " );
765                         g_is_engine = false;
766                         return TTSD_ERROR_ENGINE_NOT_FOUND;
767                 } else {
768                         g_is_engine = true;
769                 }
770         }
771
772         if (-1 != ttsd_setting_data_is_setting(uid)) {
773                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] pid has already been registered ");
774                 return TTSD_ERROR_INVALID_PARAMETER;
775         }
776
777         if (0 == ttsd_data_get_client_count()) {
778                 if( 0 != ttsd_engine_agent_load_current_engine() ) {
779                         SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to load current engine ");
780                         return TTSD_ERROR_OPERATION_FAILED;
781                 }
782         }
783
784         /* register pid */
785         if (0 != ttsd_setting_data_add(uid)) {
786                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to add client info ");
787                 return TTSD_ERROR_OPERATION_FAILED;
788         }
789
790         return TTSD_ERROR_NONE;
791 }
792
793 int ttsd_server_setting_finalize(int uid)
794 {
795         if (-1 == ttsd_setting_data_is_setting(uid)) {
796                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
797                 return TTSD_ERROR_INVALID_PARAMETER;
798         }
799
800         ttsd_setting_data_delete(uid);
801
802         /* unload engine, if ref count of client is 0 */
803         if (0 == ttsd_data_get_client_count())
804         {
805                 if (0 != ttsd_engine_agent_release()) {
806                         SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to release engine agent");
807                 } else {
808                         SLOG(LOG_DEBUG, TAG_TTSD, "[Server Setting SUCCESS] Release engine agent");
809                 }
810                 ecore_timer_add(0, __quit_ecore_loop, NULL);
811         }
812
813         return TTSD_ERROR_NONE;
814 }
815
816 int ttsd_server_setting_get_engine_list(int uid, GList** engine_list)
817 {
818         if (-1 == ttsd_setting_data_is_setting(uid)) {
819                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
820                 return TTSD_ERROR_INVALID_PARAMETER;
821         }
822
823         int ret = 0;
824         ret = ttsd_engine_setting_get_engine_list(engine_list);
825         if (0 != ret) {
826                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to get engine list : result(%d)", ret);
827                 return ret;
828         }
829
830         return TTSD_ERROR_NONE;
831 }
832
833 int ttsd_server_setting_get_current_engine(int uid, char** engine_id)
834 {
835         if (-1 == ttsd_setting_data_is_setting(uid)) {
836                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
837                 return TTSD_ERROR_INVALID_PARAMETER;
838         }
839
840         int ret = 0;
841         ret = ttsd_engine_setting_get_engine(engine_id);
842         if (0 != ret) {
843                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to get current engine : result(%d) ", ret);
844                 return ret;
845         }
846
847
848         return TTSD_ERROR_NONE;
849 }
850
851 bool __get_client_cb(int pid, int uid, app_state_e state, void* user_data)
852 {
853         /* clear client data */
854         ttsd_data_clear_data(uid);                      
855         ttsd_data_set_client_state(uid, APP_STATE_READY);
856
857         /* send message */
858         if ( 0 != ttsdc_send_set_state_message(pid, uid, APP_STATE_READY)) {
859                 /* remove client */
860                 ttsd_data_delete_client(uid);
861         } 
862
863         return true;
864 }
865
866 int ttsd_server_setting_set_current_engine(int uid, const char* engine_id)
867 {
868         /* check if uid is valid */
869         if (-1 == ttsd_setting_data_is_setting(uid)) {
870                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
871                 return TTSD_ERROR_INVALID_PARAMETER;
872         }
873
874         if (true == ttsd_engine_agent_is_same_engine(engine_id)) {
875                 SLOG(LOG_DEBUG, TAG_TTSD, "[Server Setting] new engine is the same as current engine ");
876                 return TTSD_ERROR_NONE;
877         }
878
879         /* stop all player */ 
880         ttsd_player_all_stop();
881
882         /* send interrupt message to  all clients */
883         ttsd_data_foreach_clients(__get_client_cb, NULL);
884
885         /* set engine */
886         int ret = 0;
887         ret = ttsd_engine_setting_set_engine(engine_id);
888         if (0 != ret) {
889                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to set current engine : result(%d) ", ret);
890                 return ret;
891         }
892
893         return TTSD_ERROR_NONE;
894 }
895
896 int ttsd_server_setting_get_voice_list(int uid, char** engine_id, GList** voice_list)
897 {
898         /* check if uid is valid */
899         if (-1 == ttsd_setting_data_is_setting(uid)) {
900                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
901                 return TTSD_ERROR_INVALID_PARAMETER;
902         }
903
904         /* get language list from engine */
905         int ret = 0;
906         ret = ttsd_engine_setting_get_voice_list(engine_id, voice_list);
907         if (0 != ret) {
908                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to get voice list : result(%d)", ret);
909                 return ret;
910         }
911
912         return TTSD_ERROR_NONE;
913 }
914
915 int ttsd_server_setting_get_default_voice(int uid, char** language, ttsp_voice_type_e* vctype)
916 {
917         /* check if uid is valid */
918         if (-1 == ttsd_setting_data_is_setting(uid)) {
919                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
920                 return TTSD_ERROR_INVALID_PARAMETER;
921         }
922         
923         int ret = 0;
924         ret = ttsd_engine_setting_get_default_voice(language, vctype);
925         if (0 != ret) {
926                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] Fail to get default voice : result(%d) ", ret);
927                 return ret;
928         }
929
930         return TTSD_ERROR_NONE;
931 }
932
933 int ttsd_server_setting_set_default_voice(int uid, const char* language, int vctype)
934 {
935         /* check if uid is valid */
936         if (-1 == ttsd_setting_data_is_setting(uid)) {
937                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
938                 return TTSD_ERROR_INVALID_PARAMETER;
939         }
940
941         /* set current language */
942         int ret = 0;
943         ret = ttsd_engine_setting_set_default_voice((const char*)language, (const ttsp_voice_type_e)vctype);
944         if (0 != ret) {
945                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to set default voice : result(%d) ", ret);
946                 return ret;
947         }       
948
949         return TTSD_ERROR_NONE;
950 }
951
952 int ttsd_server_setting_get_engine_setting(int uid, char** engine_id, GList** engine_setting_list)
953 {
954         /* check if uid is valid */
955         if (-1 == ttsd_setting_data_is_setting(uid)) {
956                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
957                 return TTSD_ERROR_INVALID_PARAMETER;
958         }
959
960         int ret = 0;
961         ret = ttsd_engine_setting_get_engine_setting_info(engine_id, engine_setting_list);
962         if (0 != ret) {
963                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to get engine setting info : result(%d)", ret);
964                 return ret;
965         }
966
967         return TTSD_ERROR_NONE;
968 }
969
970 int ttsd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
971 {
972         /* check if uid is valid */
973         if (-1 == ttsd_setting_data_is_setting(uid)) {
974                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
975                 return TTSD_ERROR_INVALID_PARAMETER;
976         }
977
978         int ret = 0;
979         ret = ttsd_engine_setting_set_engine_setting(key, value);
980         if (0 != ret) {
981                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to set engine setting info : result(%d)", ret);
982                 return ret;
983         }
984
985         return TTSD_ERROR_NONE;
986 }
987
988 int ttsd_server_setting_get_default_speed(int uid, int* default_speed)
989 {
990         /* check if uid is valid */
991         if (-1 == ttsd_setting_data_is_setting(uid)) {
992                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
993                 return TTSD_ERROR_INVALID_PARAMETER;
994         }
995
996         /* get current speed */
997         int ret = 0;
998         ret = ttsd_engine_setting_get_default_speed((ttsp_speed_e*)default_speed);
999         if (0 != ret) {
1000                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to get default speed : result(%d)", ret);
1001                 return ret;
1002         }       
1003
1004         return TTSD_ERROR_NONE;
1005 }
1006
1007 int ttsd_server_setting_set_default_speed(int uid, int default_speed)
1008 {
1009         /* check if uid is valid */
1010         if (-1 == ttsd_setting_data_is_setting(uid)) {
1011                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] uid is not valid  (%s)", uid);
1012                 return TTSD_ERROR_INVALID_PARAMETER;
1013         }
1014
1015         /* set default speed */
1016         int ret = 0;
1017         ret = ttsd_engine_setting_set_default_speed((ttsp_speed_e)default_speed);
1018         if (0 != ret) {
1019                 SLOG(LOG_ERROR, TAG_TTSD, "[Server Setting ERROR] fail to set default speed : result(%d)", ret);
1020                 return ret;
1021         }       
1022
1023         return TTSD_ERROR_NONE;
1024 }
1025
1026