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