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