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