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