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