Add new class for managing state of server
[platform/core/uifw/tts.git] / server / ttsd_server.c
1 /*
2 *  Copyright (c) 2011-2016 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 <app_manager.h>
15 #include <aul.h>
16 #include <Ecore.h>
17 #include <sys/time.h>
18
19 #include "ttsd_config.h"
20 #include "ttsd_data.h"
21 #include "ttsd_ipc.h"
22 #include "ttsd_engine_agent.h"
23 #include "ttsd_main.h"
24 #include "ttsd_player.h"
25 #include "ttsd_state.h"
26 #include "ttsd_server.h"
27
28
29 #define CLIENT_CLEAN_UP_TIME 500
30
31 /* for checking time */
32 #define TIME_DIFF(start, end) \
33         ((uint64_t)((end).tv_sec - (start).tv_sec) * 1000000 + (((end).tv_nsec - (start).tv_nsec) / 1000)) / 1000
34
35 static struct timespec g_request_playing, g_start_playing;
36 static double g_avg_latency;
37 static double g_min_latency;
38 static double g_max_latency;
39 static int g_file_num;
40
41 typedef struct {
42         unsigned int uid;
43         int uttid;
44 } utterance_t;
45
46 /* If current engine exist */
47 //static bool   g_is_engine;
48
49 static Ecore_Timer* g_check_client_timer = NULL;
50 static Ecore_Timer* g_wait_timer = NULL;
51 static Ecore_Timer* g_quit_loop_timer = NULL;
52
53 static utterance_t g_utt;
54
55 static GList *g_proc_list = NULL;
56
57 static bool g_is_terminated = false;
58
59 static int g_activated_mode = 0;
60
61 /* Function definitions */
62 static int __stop_and_send_ready_state(unsigned int uid)
63 {
64         int ret = ttsd_server_stop(uid);
65         if (TTSD_ERROR_NONE != ret) {
66                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
67                 return ret;
68         }
69
70         int pid = ttsd_data_get_pid(uid);
71         if (pid <= 0) {
72                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
73                 return TTSD_ERROR_OPERATION_FAILED;
74         } else {
75                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
76         }
77
78         return ret;
79 }
80
81 static void __synthesis(unsigned int uid);
82
83 static Eina_Bool __wait_synthesis(void *data)
84 {
85         /* get current play */
86         unsigned int uid = ttsd_data_get_current_playing();
87
88         if (uid > 0) {
89                 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
90                 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
91                         return EINA_TRUE;
92                 } else {
93                         SLOG(LOG_INFO, tts_tag(), "[Server] synth_control(%d)", synth_control);
94                         g_wait_timer = NULL;
95                         if (TTSD_SYNTHESIS_CONTROL_DONE == synth_control) {
96                                 /* Start next synthesis */
97                                 __synthesis(uid);
98                         }
99                 }
100         } else {
101                 g_wait_timer = NULL;
102         }
103
104         return EINA_FALSE;
105 }
106
107 static void __synthesis(unsigned int uid)
108 {
109         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  START");
110
111         speak_data_s* speak_data = NULL;
112         int ret = ttsd_data_get_speak_data(uid, &speak_data);
113         if (TTSD_ERROR_NONE != ret || NULL == speak_data) {
114                 SLOG(LOG_DEBUG, tts_tag(), "@@@ Fail to get speak data. ret(%d)", ret);
115                 SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
116                 return;
117         }
118
119         int pid = ttsd_data_get_pid(uid);
120         if (pid <= 0) {
121                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
122                 ttsd_data_destroy_speak_data(speak_data);
123                 speak_data = NULL;
124                 return;
125         }
126
127         char appid[1024] = {0, };
128         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
129                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id");
130         }
131
132         if (NULL == speak_data->lang || NULL == speak_data->text) {
133                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
134                 __stop_and_send_ready_state(uid);
135                 ttsd_data_destroy_speak_data(speak_data);
136                 speak_data = NULL;
137                 return;
138         }
139
140         g_utt.uid = uid;
141         g_utt.uttid = speak_data->utt_id;
142
143         char* credential = ttsd_data_get_credential(uid);
144
145         SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
146         SLOG(LOG_INFO, tts_tag(), "ID : uid (%u), uttid(%d) ", g_utt.uid, g_utt.uttid);
147         SLOG(LOG_INFO, tts_tag(), "Voice : langauge(%s), type(%d), speed(%d)", speak_data->lang, speak_data->vctype, speak_data->speed);
148         SLOG(LOG_INFO, tts_tag(), "Text : %s", speak_data->text);
149         SLOG(LOG_INFO, tts_tag(), "Credential : %s", credential);
150         SLOG(LOG_INFO, tts_tag(), "-----------------------------------------------------------");
151
152         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DOING);
153         ret = ttsd_engine_start_synthesis(speak_data->lang, speak_data->vctype, speak_data->text, speak_data->speed, appid, credential, NULL);
154         if (TTSD_ERROR_NONE != ret) {
155                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
156
157                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
158                 __stop_and_send_ready_state(uid);
159         } else {
160                 g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
161         }
162
163         if (ttsd_data_get_synth_control() == TTSD_SYNTHESIS_CONTROL_DOING && ttsd_state_get_state() == TTSD_STATE_READY) {
164                 ttsd_state_set_state(TTSD_STATE_SYNTHESIZING);
165         }
166
167         free(credential);
168         credential = NULL;
169         ttsd_data_destroy_speak_data(speak_data);
170         speak_data = NULL;
171         SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS  END");
172
173         return;
174 }
175
176 /*
177 * TTS Server Callback Functions
178 */
179 int ttsd_send_error(ttse_error_e error, const char* msg)
180 {
181         unsigned int uid = g_utt.uid;
182         int uttid = g_utt.uttid;
183         int tmp_pid = ttsd_data_get_pid(uid);
184
185         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Error msg from engine, pid(%d), uid(%u), uttid(%d), error(%d), msg(%s)", tmp_pid, uid, uttid, error, (NULL == msg ? "NULL" : msg));
186
187         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
188
189         if (0 != ttsd_data_clear_data(uid))
190                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_clear_data()");
191
192         if (tmp_pid > 0) {
193                 if (0 != ttsdc_ipc_send_error_message(tmp_pid, uid, uttid, error, (char*)msg))
194                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_error_message()");
195         }
196
197         if (0 != ttsd_data_set_client_state(uid, APP_STATE_READY))
198                 SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_data_set_client_state()");
199
200         if (tmp_pid > 0) {
201                 if (0 != ttsdc_ipc_send_set_state_message(tmp_pid, uid, APP_STATE_READY))
202                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsdc_ipc_send_set_state_message()");
203         }
204         return 0;
205 }
206
207 int ttsd_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
208 {
209         SLOG(LOG_DEBUG, tts_tag(), "@@@ SEND SYNTHESIS RESULT START");
210
211         unsigned int uid = g_utt.uid;
212         int uttid = g_utt.uttid;
213
214         if (false == ttsd_data_is_uttid_valid(uid, uttid)) {
215                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
216                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] uttid is NOT valid !!!! - uid(%u), uttid(%d)", uid, uttid);
217                 SLOG(LOG_DEBUG, tts_tag(), "@@@");
218                 return TTSD_ERROR_INVALID_PARAMETER;
219         }
220
221         if (TTSE_RESULT_EVENT_START > event || TTSE_RESULT_EVENT_FINISH < event) {
222                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
223                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
224                 return TTSD_ERROR_INVALID_PARAMETER;
225         }
226
227         if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
228                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
229                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
230                 SLOG(LOG_DEBUG, tts_tag(), "@@@");
231                 return TTSD_ERROR_INVALID_PARAMETER;
232         }
233
234         /* check the current state */
235         app_tts_state_e state = ttsd_data_get_client_state(uid);
236         if (APP_STATE_NONE == state) {
237                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
238                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
239                 return TTSD_ERROR_INVALID_PARAMETER;
240         }
241
242         SLOG(LOG_INFO, tts_tag(), "[Server] uid(%u), current state(%d)", uid, state);
243         if (APP_STATE_CREATED == state || APP_STATE_READY == state) {
244                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
245                 SLOG(LOG_WARN, tts_tag(), "[SERVER WARNING] Current state is %d. The synthesized result is ignored.", state);
246                 return TTSD_ERROR_NONE;
247         }
248
249         /* Synthesis is success */
250         if (TTSE_RESULT_EVENT_START == event) {
251                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
252                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
253                         uid, uttid, data, data_size, audio_type, rate);
254
255                 clock_gettime(CLOCK_MONOTONIC_RAW, &g_start_playing);
256                 long long int latency = (uint64_t)TIME_DIFF(g_request_playing, g_start_playing);
257                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Latency (Request playing ~ Get 1st synthesized results): %lld ms", latency);
258                 double d_latency = (double)latency;
259                 g_avg_latency += d_latency;
260                 g_min_latency = (d_latency < g_min_latency) ? d_latency : g_min_latency;
261                 g_max_latency = (d_latency > g_max_latency) ? d_latency : g_max_latency;
262                 g_file_num++;
263                 SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
264         } else if (TTSE_RESULT_EVENT_FINISH == event) {
265                 SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
266                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Result Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
267                         uid, uttid, data, data_size, audio_type, rate);
268         } else {
269                 /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
270         }
271
272         /* add wav data */
273         sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
274         if (NULL == sound_data) {
275                 SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
276                 return TTSD_ERROR_OUT_OF_MEMORY;
277         }
278
279         if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
280                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
281                 ttsd_data_destroy_sound_data(sound_data);
282
283                 return TTSD_ERROR_OPERATION_FAILED;
284         }
285
286         if (event == TTSE_RESULT_EVENT_FINISH) {
287                 ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
288         }
289
290         return TTSD_ERROR_NONE;
291 }
292
293 static bool __get_client_cb(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
294 {
295         if (0 > ttsd_data_is_client(uid)) {
296                 SLOG(LOG_ERROR, tts_tag(), "[Server] Client(%d) is not valid.", uid);
297                 ttsd_data_delete_client(uid);
298                 return true;
299         }
300
301         /* clear client data */
302         ttsd_data_clear_data(uid);
303
304         if (APP_STATE_READY != state) {
305                 ttsd_data_set_client_state(uid, APP_STATE_READY);
306                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
307         }
308
309         return true;
310 }
311
312 static void __stop_all_client()
313 {
314         SLOG(LOG_INFO, tts_tag(), "[Server] Send to stop all requests");
315
316         ttsd_engine_cancel_synthesis();
317         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
318
319         /* stop all player */
320         ttsd_player_all_stop();
321
322         /* send interrupt message to  all clients */
323         ttsd_data_foreach_clients(__get_client_cb, NULL);
324 }
325
326 static void __config_changed_cb(tts_config_type_e type, const char* str_param, int int_param, double double_param)
327 {
328         switch (type) {
329         case TTS_CONFIG_TYPE_ENGINE:
330         {
331                 /* TODO - Determine the policy when engine process get engine changed cb */
332                 if (NULL == str_param) {
333                         SLOG(LOG_ERROR, tts_tag(), "[Server] engine id from config is NULL");
334                         return;
335                 }
336
337                 int ret = 0;
338                 if (true == ttsd_engine_agent_is_same_engine(str_param)) {
339                         SLOG(LOG_DEBUG, tts_tag(), "[Server Setting] new engine is the same as current engine");
340                         ret = ttsd_engine_agent_unload_current_engine();
341                         if (0 != ret) {
342                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload current engine : result(%d)", ret);
343                         }
344
345                         ret = ttsd_engine_agent_load_current_engine();
346                         if (0 != ret) {
347                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine : result (%d)", ret);
348                         }
349                         return;
350                 }
351
352                 __stop_all_client();
353                 break;
354         }
355
356         case TTS_CONFIG_TYPE_VOICE:
357         {
358                 if (NULL == str_param) {
359                         SLOG(LOG_ERROR, tts_tag(), "[Server] language from config is NULL");
360                         return;
361                 }
362
363                 char* out_lang = NULL;
364                 int out_type;
365                 int ret = -1;
366
367                 if (true == ttsd_engine_select_valid_voice(str_param, int_param, &out_lang, &out_type)) {
368                         SLOG(LOG_ERROR, tts_tag(), "[Server] valid language : lang(%s), type(%d)", out_lang, out_type);
369                         ret = ttsd_engine_agent_set_default_voice(out_lang, out_type);
370                         if (0 != ret)
371                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set valid language : lang(%s), type(%d)", out_lang, out_type);
372                 } else {
373                         /* Current language is not available */
374                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set voice : lang(%s), type(%d)", str_param, int_param);
375                 }
376                 if (NULL != out_lang) {
377                         free(out_lang);
378                         out_lang = NULL;
379                 }
380                 break;
381         }
382
383         case TTS_CONFIG_TYPE_SPEED:
384         {
385                 if (TTS_SPEED_MIN <= int_param && int_param <= TTS_SPEED_MAX) {
386                         /* set default speed */
387                         int ret = 0;
388                         ret = ttsd_engine_agent_set_default_speed(int_param);
389                         if (0 != ret) {
390                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default speed : result(%d)", ret);
391                         }
392                 }
393                 break;
394         }
395
396         case TTS_CONFIG_TYPE_PITCH:
397         {
398                 if (TTS_PITCH_MIN <= int_param && int_param <= TTS_PITCH_MAX) {
399                         /* set default speed */
400                         int ret = 0;
401                         ret = ttsd_engine_agent_set_default_pitch(int_param);
402                         if (0 != ret) {
403                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default pitch : result(%d)", ret);
404                         }
405                 }
406                 break;
407         }
408
409         case TTS_CONFIG_TYPE_BACKGROUND_VOLUME_RATIO:
410         {
411                 if (0.0 <= double_param && double_param <= 1.0) {
412                         /* set default bg volume ratio */
413                         int ret = 0;
414                         ret = ttsd_player_set_background_volume_ratio(double_param);
415                         if (0 != ret) {
416                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set default bg volume ratio : result(%d)", ret);
417                         }
418                 }
419                 break;
420         }
421         }
422
423         return;
424 }
425
426 bool __terminate_client(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
427 {
428         SLOG(LOG_INFO, tts_tag(), "@@@ Start to terminate client [%d]", uid);
429         ttsd_server_finalize(uid);
430         return true;
431 }
432
433 static int __terminate_server()
434 {
435         if (g_is_terminated) {
436                 SLOG(LOG_INFO, tts_tag(), "[INFO] ttsd_terminate() is already invoked.");
437                 return TTSD_ERROR_NONE;
438         }
439
440         int ret = TTSD_ERROR_NONE;
441         ret = ttsd_ipc_close_connection();
442         if (TTSD_ERROR_NONE != ret) {
443                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_ipc_close_connection(). ret(%d)", ret);
444                 return TTSD_ERROR_OPERATION_FAILED;
445         }
446
447         ret = ttsd_finalize();
448         if (TTSD_ERROR_NONE != ret) {
449                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_finalize(). ret(%d)", ret);
450                 return TTSD_ERROR_OPERATION_FAILED;
451         }
452
453         g_is_terminated = true;
454         return TTSD_ERROR_NONE;
455 }
456
457 static Eina_Bool __quit_ecore_loop(void *data)
458 {
459         __terminate_server();
460         ecore_main_loop_quit();
461
462         return EINA_FALSE;
463 }
464
465 void __screen_reader_changed_cb(bool value)
466 {
467         SLOG(LOG_DEBUG, tts_tag(), "[Server] Screen reader is %s", value ? "ON" : "OFF");
468         return;
469 }
470
471 static void __read_proc()
472 {
473         DIR *dp = NULL;
474         struct dirent *dirp = NULL;
475         int tmp;
476
477         GList *iter = NULL;
478         if (0 < g_list_length(g_proc_list)) {
479                 iter = g_list_first(g_proc_list);
480                 while (NULL != iter) {
481                         g_proc_list = g_list_remove_link(g_proc_list, iter);
482                         g_list_free(iter);
483                         iter = g_list_first(g_proc_list);
484                 }
485         }
486
487         dp = opendir("/proc");
488         if (NULL == dp) {
489                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open proc");
490         } else {
491                 do {
492                         dirp = readdir(dp);
493
494                         if (NULL != dirp) {
495                                 tmp = atoi(dirp->d_name);
496                                 if (0 >= tmp)   continue;
497                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
498                         }
499                 } while (NULL != dirp);
500                 closedir(dp);
501         }
502         return;
503 }
504
505 bool __get_client_for_clean_up(int pid, unsigned int uid, app_tts_state_e state, void* user_data)
506 {
507         bool exist = false;
508         int i = 0;
509
510         GList *iter = NULL;
511         for (i = 0; i < g_list_length(g_proc_list); i++) {
512                 iter = g_list_nth(g_proc_list, i);
513                 if (NULL != iter) {
514                         if (pid == GPOINTER_TO_INT(iter->data)) {
515                                 SLOG(LOG_DEBUG, tts_tag(), "uid (%u) is running", uid);
516                                 exist = true;
517                                 break;
518                         }
519                 }
520         }
521
522         if (false == exist) {
523                 SLOG(LOG_ERROR, tts_tag(), "uid (%u) should be removed", uid);
524                 ttsd_server_finalize(uid);
525         }
526
527         return true;
528 }
529
530 static Eina_Bool __cleanup_client(void *data)
531 {
532         SLOG(LOG_DEBUG, tts_tag(), "@@@ CLEAN UP CLIENT START");
533         __read_proc();
534
535         if (0 < ttsd_data_get_client_count()) {
536                 ttsd_data_foreach_clients(__get_client_for_clean_up, NULL);
537         } else {
538                 if (NULL == g_quit_loop_timer) {
539                         SLOG(LOG_INFO, tts_tag(), "[Server] Add a timer __quit_ecore_loop");
540                         g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
541                 }
542                 SLOG(LOG_ERROR, tts_tag(), "[Server] Deleted timer");
543                 g_check_client_timer = NULL;
544                 return EINA_FALSE;
545         }
546
547         SLOG(LOG_DEBUG, tts_tag(), "@@@");
548
549         return EINA_TRUE;
550 }
551
552 /*
553 * Server APIs
554 */
555 int ttsd_initialize(ttse_request_callback_s *callback)
556 {
557         SLOG(LOG_INFO, tts_tag(), "[Server] Initialize");
558         g_is_terminated = false;
559
560         if (ttsd_config_initialize(__config_changed_cb)) {
561                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize config.");
562         }
563
564         if (TTSD_ERROR_NONE != ttsd_state_initialize(NULL)) {
565                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to initialize state.");
566         }
567
568         /* player init */
569         if (ttsd_player_init()) {
570                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to initialize player init.");
571                 return TTSD_ERROR_OPERATION_FAILED;
572         }
573
574         double ratio;
575         if (0 == ttsd_config_get_bg_volume_ratio(&ratio))
576         {
577                 SLOG(LOG_ERROR, tts_tag(), "[Server] get bg volume ratio is %lf", ratio);
578                 int ret = ttsd_player_set_background_volume_ratio(ratio);
579                 if (0 != ret)
580                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set bg volume ratio : result(%d)", ret);
581         } else {
582                 SLOG(LOG_ERROR, tts_tag(), "[Server WARNING] Fail to get bg volume ratio");
583         }
584
585         /* Engine Agent initialize */
586         if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
587                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
588                 return TTSD_ERROR_OPERATION_FAILED;
589         }
590
591         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
592         ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
593
594         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, __cleanup_client, NULL);
595         if (NULL == g_check_client_timer) {
596                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
597         }
598
599         g_activated_mode = 0;
600
601         return TTSD_ERROR_NONE;
602 }
603
604 int ttsd_finalize()
605 {
606         SLOG(LOG_INFO, tts_tag(), "[Server] Finalize");
607
608         GList *iter = NULL;
609         if (0 < g_list_length(g_proc_list)) {
610                 iter = g_list_first(g_proc_list);
611                 while (NULL != iter) {
612                         g_proc_list = g_list_remove_link(g_proc_list, iter);
613                         g_list_free(iter);
614                         iter = g_list_first(g_proc_list);
615                 }
616         }
617
618         if (g_wait_timer) {
619                 ecore_timer_del(g_wait_timer);
620                 g_wait_timer = NULL;
621                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore waiting timer handle");
622         }
623
624         if (g_check_client_timer) {
625                 ecore_timer_del(g_check_client_timer);
626                 g_check_client_timer = NULL;
627                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore checking client timer handle");
628         }
629
630         if (g_quit_loop_timer) {
631                 ecore_timer_del(g_quit_loop_timer);
632                 g_quit_loop_timer = NULL;
633                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
634         }
635
636         ttsd_config_finalize();
637         ttsd_state_finalize();
638
639         int ret = TTSD_ERROR_NONE;
640         ret = ttsd_player_release();
641         if (TTSD_ERROR_NONE != ret) {
642                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_player_release(). ret(%d)", ret);
643                 return TTSD_ERROR_OPERATION_FAILED;
644         }
645
646         ret = ttsd_engine_agent_release();
647         if (TTSD_ERROR_NONE != ret) {
648                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_engine_agent_release(). ret(%d)", ret);
649                 return TTSD_ERROR_OPERATION_FAILED;
650         }
651
652         return TTSD_ERROR_NONE;
653 }
654
655 int ttsd_terminate()
656 {
657         SLOG(LOG_INFO, tts_tag(), "[Server] Terminate");
658         int ret = ttsd_data_foreach_clients(__terminate_client, NULL);
659         if (TTSD_ERROR_NONE != ret) {
660                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to terminate all clients. ret(%d)", ret);
661                 return TTSD_ERROR_OPERATION_FAILED;
662         }
663
664         if (g_quit_loop_timer) {
665                 ecore_timer_del(g_quit_loop_timer);
666                 g_quit_loop_timer = NULL;
667                 SLOG(LOG_INFO, tts_tag(), "[INFO] Delete ecore quit loop timer handle");
668         }
669
670         return __terminate_server();
671 }
672
673 /*
674 * TTS Server Functions for Client
675 */
676
677 int ttsd_server_is_already_initialized(int pid, unsigned int uid, bool* is_initialized)
678 {
679         if (-1 != ttsd_data_is_client(uid))
680                 *is_initialized = true;
681         else
682                 *is_initialized = false;
683
684         SLOG(LOG_INFO, tts_tag(), "[Server INFO] Pid(%d), uid(%u) is %s", pid, uid, *is_initialized ? "already initialized" : "not initialized yet");
685         return TTSD_ERROR_NONE;
686 }
687
688 static void __set_and_notify_activated_mode()
689 {
690         int activated_mode = ttsd_data_get_activated_mode();
691         if (g_activated_mode == activated_mode) {
692                 return;
693         }
694
695         SLOG(LOG_INFO, tts_tag(), "[Server] Activated mode is changed from(%d) to (%d)", g_activated_mode, activated_mode);
696
697         g_activated_mode = activated_mode;
698         int ret = ttsd_engine_notify_activated_mode_changed(g_activated_mode);
699         if (TTSD_ERROR_NONE != ret) {
700                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to notify activated mode changed event.(%d)", ret);
701         }
702 }
703
704 static int __check_app_agreed(int pid, unsigned int uid, bool credential_needed)
705 {
706         if (false == credential_needed) {
707                 return TTSD_ERROR_NONE;
708         }
709
710         char* appid = NULL;
711         if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
712                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get app id, pid(%d)", pid);
713         }
714
715         bool is_agreed = false;
716         int ret = ttsd_engine_check_app_agreed(appid, &is_agreed);
717         free(appid);
718         appid = NULL;
719
720         if (TTSD_ERROR_NONE != ret) {
721                 SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
722                 return TTSD_ERROR_OPERATION_FAILED;
723         }
724
725         if (false == is_agreed) {
726                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
727                 return TTSD_ERROR_PERMISSION_DENIED;
728         }
729
730         return TTSD_ERROR_NONE;
731 }
732
733 int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_method_e method, bool* credential_needed)
734 {
735         SLOG(LOG_INFO, tts_tag(), "[Server] Server initialize");
736
737         if (-1 != ttsd_data_is_client(uid)) {
738                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Uid has already been registered");
739                 return TTSD_ERROR_NONE;
740         }
741
742         if (TTSD_ERROR_NONE != ttsd_data_new_client(pid, uid, mode, method)) {
743                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
744                 return TTSD_ERROR_OPERATION_FAILED;
745         }
746
747         __set_and_notify_activated_mode();
748
749         if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
750                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
751                 ttsd_data_delete_client(uid);
752                 return TTSD_ERROR_OPERATION_FAILED;
753         }
754
755         bool is_needed = false;
756         if (TTSD_ERROR_NONE != ttsd_engine_agent_is_credential_needed(uid, &is_needed)) {
757                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
758                 ttsd_data_delete_client(uid);
759                 return TTSD_ERROR_OPERATION_FAILED;
760         }
761
762         int ret = __check_app_agreed(pid, uid, is_needed);
763         if (TTSD_ERROR_NONE != ret) {
764                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Checking credential is finished(%d)", ret);
765                 ttsd_data_delete_client(uid);
766                 return ret;
767         }
768
769         *credential_needed = is_needed;
770
771         return TTSD_ERROR_NONE;
772 }
773
774 void __used_voice_cb(const char* lang, int type)
775 {
776         SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to unload voice (%s,%d)", lang, type);
777         if (0 != ttsd_engine_unload_voice(lang, type)) {
778                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to unload voice");
779         }
780 }
781
782 int ttsd_server_finalize(unsigned int uid)
783 {
784         SLOG(LOG_INFO, tts_tag(), "[Server] Server finalize");
785         SLOG(LOG_INFO, tts_tag(), "[Server] File num(%d), Avg Latency(%lf), Min(%lf), Max(%lf)", g_file_num, (g_avg_latency / (double)g_file_num), g_min_latency, g_max_latency);
786
787         if (0 > ttsd_data_is_client(uid)) {
788                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_finalize : uid is not valid");
789         }
790
791         int ret = ttsd_server_stop(uid);
792         if (TTSD_ERROR_NONE != ret) {
793                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS server top(%u) fail (%d/%s) <<<<<", uid, ret, get_error_message(ret));
794         }
795
796         /* Need to unload voice when used voice is unregistered */
797         if (0 != ttsd_data_reset_used_voice(uid, __used_voice_cb)) {
798                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set used voice");
799                 return TTSD_ERROR_OPERATION_FAILED;
800         }
801
802         ttsd_data_delete_client(uid);
803         __set_and_notify_activated_mode();
804
805         /* unload engine, if ref count of client is 0 */
806         if (0 == ttsd_data_get_client_count()) {
807                 if (NULL == g_quit_loop_timer) {
808                         SLOG(LOG_DEBUG, tts_tag(), "[Server] Quit main loop");
809                         g_quit_loop_timer = ecore_timer_add(0, __quit_ecore_loop, NULL);
810                 } else {
811                         SLOG(LOG_WARN, tts_tag(), "[Server] Already called __quit_ecore_loop");
812                 }
813         }
814
815         return TTSD_ERROR_NONE;
816 }
817
818 static bool __is_connected_to_network()
819 {
820         /* Check network */
821         int network_status = 0;
822         int ret = vconf_get_int(VCONFKEY_NETWORK_STATUS, &network_status);
823         if (0 != ret) {
824                 SLOG(LOG_WARN, tts_tag(), "[Network] Fail to get network status, ret(%d)", ret);
825                 return false;
826         }
827
828         if (network_status == VCONFKEY_NETWORK_OFF) {
829                 SLOG(LOG_WARN, tts_tag(), "[Network] Current network connection is OFF.");
830                 return false;
831         }
832
833         SLOG(LOG_DEBUG, tts_tag(), "[Network] Network status is %d", network_status);
834
835         return true;
836 }
837
838 int ttsd_server_add_text(unsigned int uid, const char* text, const char* lang, int voice_type, int speed, int utt_id, const char* credential)
839 {
840         app_tts_state_e state = ttsd_data_get_client_state(uid);
841         if (APP_STATE_NONE == state) {
842                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_add_text : uid is not valid");
843                 return TTSD_ERROR_INVALID_PARAMETER;
844         }
845
846         /* check valid voice */
847         char* temp_lang = NULL;
848         int temp_type;
849         if (true != ttsd_engine_select_valid_voice((const char*)lang, voice_type, &temp_lang, &temp_type)) {
850                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice");
851                 if (NULL != temp_lang) {
852                         free(temp_lang);
853                         temp_lang = NULL;
854                 }
855                 return TTSD_ERROR_INVALID_VOICE;
856         }
857
858         if (NULL == temp_lang) {
859                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to select valid voice : result lang is NULL");
860                 return TTSD_ERROR_INVALID_VOICE;
861         }
862
863         speak_data_s* speak_data = ttsd_data_create_speak_data(text, lang, voice_type, speed, utt_id);
864         if (NULL == speak_data) {
865                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to allocate memory");
866                 if (NULL != temp_lang) {
867                         free(temp_lang);
868                         temp_lang = NULL;
869                 }
870
871                 return TTSD_ERROR_OPERATION_FAILED;
872         }
873
874         SLOG(LOG_INFO, tts_tag(), "[Server] Add queue, lang(%s), vctype(%d), speed(%d), uttid(%d), credential(%s)", lang, voice_type, speed, utt_id, credential);
875
876         /* if state is APP_STATE_READY , APP_STATE_PAUSED , only need to add speak data to queue*/
877         int ret = -1;
878         ret = ttsd_data_add_speak_data(uid, speak_data);
879         if (0 != ret) {
880                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add speak data");
881                 if (NULL != temp_lang) {
882                         free(temp_lang);
883                         temp_lang = NULL;
884                 }
885
886                 ttsd_data_destroy_speak_data(speak_data);
887                 speak_data = NULL;
888
889                 return ret;
890         }
891
892         if (0 != ttsd_data_set_used_voice(uid, temp_lang, temp_type)) {
893                 /* Request load voice */
894                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Request to load voice");
895                 if (0 != ttsd_engine_load_voice(temp_lang, temp_type)) {
896                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load voice");
897                 }
898         }
899
900         if (NULL != temp_lang) {
901                 free(temp_lang);
902                 temp_lang = NULL;
903         }
904
905         if (APP_STATE_PLAYING == state) {
906                 /* check if engine use network */
907                 if (ttsd_engine_agent_need_network()) {
908                         if (false == __is_connected_to_network()) {
909                                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network.");
910                                 return TTSD_ERROR_OPERATION_FAILED;
911                         }
912                 }
913
914                 ttsd_data_set_credential(uid, credential);
915
916                 /* Check whether tts-engine is running or not */
917                 ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
918                 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
919                 if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
920                         SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
921                 } else {
922                         __synthesis(uid);
923                 }
924         }
925
926         return TTSD_ERROR_NONE;
927 }
928
929 static int __interrupt_player_by_policy(unsigned int request_app_uid)
930 {
931         unsigned int playing_app_uid = ttsd_data_get_current_playing();
932         if (0 > ttsd_data_is_client(playing_app_uid)) {
933                 SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
934                 return TTSD_ERROR_NONE;
935         }
936
937         if (request_app_uid == playing_app_uid) {
938                 SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
939                 return TTSD_ERROR_NONE;
940         }
941
942         ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
943         ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
944         switch (playing_app_mode) {
945         case TTSD_MODE_DEFAULT:
946         case TTSD_MODE_NOTIFICATION:
947         case TTSD_MODE_SCREEN_READER:
948                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
949                 __stop_and_send_ready_state(playing_app_uid);
950                 break;
951
952         case TTSD_MODE_INTERRUPT:
953                 if (TTSD_MODE_INTERRUPT != request_app_mode) {
954                         SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
955                         return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
956                 }
957
958                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
959                 __stop_and_send_ready_state(playing_app_uid);
960                 break;
961         }
962
963         return TTSD_ERROR_NONE;
964 }
965
966 int ttsd_server_play(unsigned int uid, const char* credential)
967 {
968         app_tts_state_e state = ttsd_data_get_client_state(uid);
969         if (APP_STATE_NONE == state) {
970                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
971                 return TTSD_ERROR_INVALID_PARAMETER;
972         }
973
974         if (APP_STATE_PLAYING == state) {
975                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
976                 return TTSD_ERROR_NONE;
977         }
978
979         /* check if engine use network */
980         if (ttsd_engine_agent_need_network()) {
981                 if (false == __is_connected_to_network()) {
982                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Disconnect network. Current engine needs network service!!!.");
983                         return TTSD_ERROR_OUT_OF_NETWORK;
984                 }
985         }
986
987         int ret = __interrupt_player_by_policy(uid);
988         if (TTSD_ERROR_NONE != ret) {
989                 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
990                 return ret;
991         }
992
993         /* Change current play */
994         if (0 != ttsd_player_wait_to_play(uid)) {
995                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to set to wait for playing : uid(%u)", uid);
996         }
997
998         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
999                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1000                 return TTSD_ERROR_OPERATION_FAILED;
1001         }
1002
1003         SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1004         ret = ttsd_player_play(uid);
1005         if (TTSD_ERROR_NONE != ret) {
1006                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1007                 ttsd_data_set_client_state(uid, state);
1008                 return TTSD_ERROR_OPERATION_FAILED;
1009         }
1010
1011         ttsd_data_set_credential(uid, credential);
1012
1013         /* Check whether tts-engine is running or not */
1014         clock_gettime(CLOCK_MONOTONIC_RAW, &g_request_playing);
1015         ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1016         SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1017         if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control) {
1018                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Engine has already been running.");
1019         } else {
1020                 __synthesis(uid);
1021         }
1022         ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_SYNTH);
1023
1024         return TTSD_ERROR_NONE;
1025 }
1026
1027 static void __stop_engine_synthesis(unsigned int uid)
1028 {
1029         ttsd_synthesis_control_e synth_control = ttsd_data_get_synth_control();
1030         SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
1031
1032         if (TTSD_SYNTHESIS_CONTROL_DOING == synth_control && uid == ttsd_data_get_current_playing()) {
1033                 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS-engine is running");
1034
1035                 int ret = ttsd_engine_cancel_synthesis();
1036                 if (TTSD_ERROR_NONE != ret)
1037                         SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to cancel synthesis : ret(%d)", ret);
1038         }
1039
1040         ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
1041 }
1042
1043 int ttsd_server_stop(unsigned int uid)
1044 {
1045         app_tts_state_e state = ttsd_data_get_client_state(uid);
1046         if (APP_STATE_NONE == state) {
1047                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1048                 return TTSD_ERROR_INVALID_PARAMETER;
1049         }
1050
1051         SLOG(LOG_INFO, tts_tag(), "[Server] server stop, uid(%d), state(%d)", uid, state);
1052
1053         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1054                 if (TTS_APP_PLAY_TYPE_SYNTH == ttsd_data_get_play_type(uid)) {
1055                         __stop_engine_synthesis(uid);
1056                 }
1057
1058                 /* stop player */
1059                 ttsd_data_set_client_state(uid, APP_STATE_READY);
1060                 if (TTSD_ERROR_NONE != ttsd_player_stop(uid)) {
1061                         SLOG(LOG_WARN, tts_tag(), "[Server] Fail to ttsd_player_stop()");
1062                 }
1063         }
1064
1065         /* Reset all data */
1066         ttsd_data_clear_data(uid);
1067
1068         return TTSD_ERROR_NONE;
1069 }
1070
1071 int ttsd_server_pause(unsigned int uid)
1072 {
1073         app_tts_state_e state = ttsd_data_get_client_state(uid);
1074         if (APP_STATE_NONE == state) {
1075                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_pause : uid is not valid");
1076                 return TTSD_ERROR_INVALID_PARAMETER;
1077         }
1078
1079         if (APP_STATE_PLAYING != state) {
1080                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state is not 'play'");
1081                 return TTSD_ERROR_INVALID_STATE;
1082         }
1083
1084         int ret = ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
1085         if (TTSD_ERROR_NONE != ret) {
1086                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail set pause state() : ret(%d)", ret);
1087                 return TTSD_ERROR_OPERATION_FAILED;
1088         }
1089
1090         ret = ttsd_player_pause(uid);
1091         if (TTSD_ERROR_NONE != ret) {
1092                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail player_pause() : ret(%d)", ret);
1093                 return TTSD_ERROR_OPERATION_FAILED;
1094         }
1095
1096         return TTSD_ERROR_NONE;
1097 }
1098
1099 int ttsd_server_get_support_voices(unsigned int uid, GList** voice_list)
1100 {
1101         if (0 > ttsd_data_is_client(uid)) {
1102                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid is not valid");
1103                 return TTSD_ERROR_INVALID_PARAMETER;
1104         }
1105
1106         /* get voice list*/
1107         int ret = ttsd_engine_get_voice_list(voice_list);
1108         if (0 != ret) {
1109                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1110                 return ret;
1111         }
1112
1113         SLOG(LOG_DEBUG, tts_tag(), "[Server SUCCESS] Get supported voices");
1114
1115         return TTSD_ERROR_NONE;
1116 }
1117
1118 int ttsd_server_get_current_voice(unsigned int uid, char** language, int* voice_type)
1119 {
1120         if (0 > ttsd_data_is_client(uid)) {
1121                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] ttsd_server_get_current_voice : uid is not valid");
1122                 return TTSD_ERROR_INVALID_PARAMETER;
1123         }
1124
1125         /* get current voice */
1126         int ret = ttsd_engine_get_default_voice(language, voice_type);
1127         if (0 != ret) {
1128                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail ttsd_server_get_support_voices() : ret(%d)", ret);
1129                 return ret;
1130         }
1131
1132         SLOG(LOG_DEBUG, tts_tag(), "[Server] Get default language (%s), voice type(%d) ", *language, *voice_type);
1133
1134         return TTSD_ERROR_NONE;
1135 }
1136
1137 int ttsd_server_set_private_data(unsigned int uid, const char* key, const char* data)
1138 {
1139         app_tts_state_e state = ttsd_data_get_client_state(uid);
1140         if (APP_STATE_NONE == state) {
1141                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1142                 return TTSD_ERROR_INVALID_PARAMETER;
1143         }
1144
1145         if (APP_STATE_READY != state) {
1146                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1147                 return TTSD_ERROR_INVALID_STATE;
1148         }
1149
1150         int ret = ttsd_engine_set_private_data(key, data);
1151         if (0 != ret) {
1152                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data : ret(%d)", ret);
1153         } else {
1154                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data, key(%s), data(%s)", key, data);
1155         }
1156
1157         return ret;
1158 }
1159
1160 int ttsd_server_get_private_data(unsigned int uid, const char* key, char** data)
1161 {
1162         app_tts_state_e state = ttsd_data_get_client_state(uid);
1163         if (APP_STATE_NONE == state) {
1164                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1165                 return TTSD_ERROR_INVALID_PARAMETER;
1166         }
1167
1168         if (APP_STATE_READY != state) {
1169                 SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Current state of (%u) is NOT 'READY'", uid);
1170                 return TTSD_ERROR_INVALID_STATE;
1171         }
1172
1173         char* temp_data = NULL;
1174         int ret = ttsd_engine_get_private_data(key, &temp_data);
1175         if (0 != ret) {
1176                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get private data : ret(%d)", ret);
1177         } else {
1178                 SLOG(LOG_DEBUG, tts_tag(), "[Server] Get private data, key(%s), data(%s)", key, *data);
1179         }
1180
1181         if (NULL == temp_data) {
1182                 temp_data = strdup("NULL");
1183         }
1184         *data = temp_data;
1185
1186         return ret;
1187 }
1188
1189 int ttsd_set_private_data_set_cb(ttse_private_data_set_cb callback)
1190 {
1191         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data set cb");
1192
1193         int ret = 0;
1194         ret = ttsd_engine_agent_set_private_data_set_cb(callback);
1195         if (0 != ret) {
1196                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
1197         }
1198
1199         return ret;
1200 }
1201
1202 int ttsd_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
1203 {
1204         SLOG(LOG_DEBUG, tts_tag(), "[Server] Set private data requested cb");
1205
1206         int ret = 0;
1207         ret = ttsd_engine_agent_set_private_data_requested_cb(callback);
1208         if (0 != ret) {
1209                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1210         }
1211
1212         return ret;
1213 }
1214
1215 int ttsd_get_activated_mode(int* activated_mode)
1216 {
1217         if (NULL == activated_mode) {
1218                 return TTSD_ERROR_INVALID_PARAMETER;
1219         }
1220
1221         *activated_mode = ttsd_data_get_activated_mode();
1222         return TTSD_ERROR_NONE;
1223 }
1224
1225 int ttsd_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
1226 {
1227         int ret = ttsd_engine_agent_set_activated_mode_changed_cb(callback);
1228         if (TTSD_ERROR_NONE != ret) {
1229                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
1230         }
1231
1232         return ret;
1233 }
1234
1235 int ttsd_server_play_pcm(unsigned int uid)
1236 {
1237         app_tts_state_e state = ttsd_data_get_client_state(uid);
1238         if (APP_STATE_NONE == state) {
1239                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] uid(%u) is NOT valid", uid);
1240                 return TTSD_ERROR_INVALID_PARAMETER;
1241         }
1242
1243         if (APP_STATE_PLAYING == state) {
1244                 SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current state of (%u) is 'PLAYING'", uid);
1245                 return TTSD_ERROR_NONE;
1246         }
1247
1248         int ret = __interrupt_player_by_policy(uid);
1249         if (TTSD_ERROR_NONE != ret) {
1250                 SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
1251                 return ret;
1252         }
1253
1254         /* Change current play */
1255         if (0 != ttsd_data_set_client_state(uid, APP_STATE_PLAYING)) {
1256                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to set state : uid(%u)", uid);
1257                 return TTSD_ERROR_OPERATION_FAILED;
1258         }
1259
1260         SLOG(LOG_DEBUG, tts_tag(), "[Server] Play player. uid(%u)", uid);
1261         ret = ttsd_player_play(uid);
1262         if (TTSD_ERROR_NONE != ret) {
1263                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to play sound : uid(%u)", uid);
1264                 ttsd_data_set_client_state(uid, state);
1265                 return TTSD_ERROR_OPERATION_FAILED;
1266         }
1267
1268         ttsd_data_set_play_type(uid, TTS_APP_PLAY_TYPE_PCM);
1269
1270         return TTSD_ERROR_NONE;
1271 }
1272
1273 int ttsd_server_add_pcm(unsigned int uid, int event, void* data, int data_size, int audio_type, int rate)
1274 {
1275         SLOG(LOG_DEBUG, tts_tag(), "@@@ ADD PCM");
1276
1277         int uttid = -1;
1278
1279         /* Synthesis is success */
1280         if (TTSE_RESULT_EVENT_START == event || TTSE_RESULT_EVENT_CONTINUE == event || TTSE_RESULT_EVENT_FINISH == event) {
1281                 if (TTSE_RESULT_EVENT_START == event) {
1282                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_START");
1283                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1284                                 uid, uttid, data, data_size, audio_type, rate);
1285                 } else if (TTSE_RESULT_EVENT_FINISH == event) {
1286                         SLOG(LOG_INFO, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_FINISH");
1287                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[SERVER] PCM Info : uid(%u), utt(%d), data(%p), data size(%d) audiotype(%d) rate(%d)",
1288                                 uid, uttid, data, data_size, audio_type, rate);
1289                 } else {
1290                         /*if (TTSE_RESULT_EVENT_CONTINUE == event)  SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_CONTINUE");*/
1291                 }
1292
1293                 if (rate <= 0 || audio_type < 0 || audio_type > TTSE_AUDIO_TYPE_MAX) {
1294                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] audio data is invalid");
1295                         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1296                         return TTSD_ERROR_INVALID_PARAMETER;
1297                 }
1298
1299                 sound_data_s* sound_data = ttsd_data_create_sound_data(uttid, data, data_size, event, audio_type, rate, 0);
1300                 if (NULL == sound_data) {
1301                         SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Out of memory");
1302                         return TTSD_ERROR_OUT_OF_MEMORY;
1303                 }
1304
1305                 if (0 != ttsd_data_add_sound_data(uid, sound_data)) {
1306                         SECURE_SLOG(LOG_ERROR, tts_tag(), "[SERVER ERROR] Fail to add sound data : uid(%u)", uid);
1307                         ttsd_data_destroy_sound_data(sound_data);
1308                 }
1309         } else {
1310                 SLOG(LOG_DEBUG, tts_tag(), "[SERVER] Event : TTSE_RESULT_EVENT_ERROR");
1311         }
1312
1313         SLOG(LOG_DEBUG, tts_tag(), "@@@");
1314
1315         return TTSD_ERROR_NONE;
1316 }