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