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