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